我想在光标的插入点插入标记,而不是数组的末尾
我有一个将令牌添加到 NSTokenField 的按钮。它将标记始终添加到字段的末尾:
NSTokenField *currentField = [sender representedObject];
// Determine which token should be inserted into the field using the tag of the sender.
switch( [sender tag] )
{
case eFileNameToken_StartDate:
[currentField setObjectValue:[[currentField objectValue] arrayByAddingObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:kTokenName_StartDate, kTokenKey_Name, @"%m-%d-%Y", kTokenKey_Format, [NSNumber numberWithInt:0], kTokenKey_FormatIndex, nil]]];
break;
因为它从 currentField objectValue 中获取数组,然后通过添加对象来创建数组。
我想让它知道光标的插入点并将对象插入到生成的 currentField objectValue 中,以便我可以使用正确排序的标记来设置 currentField 的ObjectValue。 感谢你们的帮助
I have a button that adds a token to a NSTokenField. It adds the token always to the end of the field:
NSTokenField *currentField = [sender representedObject];
// Determine which token should be inserted into the field using the tag of the sender.
switch( [sender tag] )
{
case eFileNameToken_StartDate:
[currentField setObjectValue:[[currentField objectValue] arrayByAddingObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:kTokenName_StartDate, kTokenKey_Name, @"%m-%d-%Y", kTokenKey_Format, [NSNumber numberWithInt:0], kTokenKey_FormatIndex, nil]]];
break;
Because it is grabbing the array from the currentField objectValue and then creating an array by adding the object.
I would love to have it know the insertion point of the cursor and insert the object into the resulting currentField objectValue so that I can then setObjectValue of the currentField with the correctly ordered tokens.
Thanks for any help yall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我想通了,我想我应该分享我的解决方案。
需要获取 fieldEditor 并检查其 selectedRange。
这将为您提供当前编辑的文本字段中的插入点。但是,如果您有一个将文本和标记混合在一起的 tokenField,那么您会发现每个标记仅算作 selectedRange 中的一个字符。
如果是这种情况,那么您需要编写一些逻辑来正确插入到 currentField 数组中。
对于种类标记的类别,1 表示
非标记字符串中的每个字符
比你的 [currentFieldEditor
selectedRange].location 并中断
跳出循环
[currentField setObjectValue: 与
该数组]
我的令牌以逗号分隔并以 $token$ 样式名称推入 NSDictionaries 中
这就是我在运行循环时划分文本和标记的方式。
快乐的 :)
So I figured it out and I thought Id share my solution.
One needs to grab the fieldEditor and check its selectedRange.
This gives you the insertion point in the currently edited text field. However if you have a tokenField that has text and tokens mixed together then you will find that each token only counts as one character in the selectedRange.
If this is the case then you need to write some logic to correctly insert into the currentField array.
for classes of kind token, 1 for
each character in a non token string
than your [currentFieldEditor
selectedRange].location and break
out of the loop
[currentField setObjectValue: with
that array]
My tokens are comma delinated and shoved into NSDictionaries with $token$ style names
This is how I delineate between text and tokens when I run my loop.
happy :)