微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – NSTextFinder以编程方式设置搜索字符串并清除视觉反馈

我有一个使用查找栏的NSTextView([textView setUsesFindBar:YES];).

我有两个问题.

>如何清除查找操作的视觉反馈?

当我以编程方式更改textView的内容时,我的问题就出现了.在内容改变之后,对先前内容搜索操作的视觉反馈保持不变.显然这些黄色框不适用于新内容,因此在更改textView内容时我需要一种方法来清除它们.

注意:我没有实现NSTextFinderClient协议,因为我有一个简单的textView,而查找栏只是工作而没有任何其他的努力.
>如何将搜索字符串发送到查找栏?

解决方法

我找到了答案,所以对于其他人来说,这是怎么做的.

首先,您需要一个NSTextFinder实例,以便您可以控制它.我们在代码中设置了它.

textFinder = [[NSTextFinder alloc] init];
[textFinder setClient:textView];
[textFinder setFindBarContainer:[textView enclosingScrollView]];
[textView setUsesFindBar:YES];
[textView setIncrementalSearchingEnabled:YES];

一个答案:为了清除视觉反馈,我可以做两件事.我可以取消视觉反馈……

[textFinder cancelFindindicator];

或者我可以提醒NSTextFinder我即将更改我的textView内容

[textFinder noteClientStringWillChange];

第二个答案:有一个全球的NSFindPboard.您可以使用它来设置搜索.

// change the NSFindPboard NSPasteboardTypestring
NSPasteboard* pBoard = [NSPasteboard pasteboardWithName:NSFindPboard];
[pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypestring,NSPasteboardTypeTextFinderOptions,nil] owner:nil];
[pBoard setString:@"new search" forType:NsstringPboardType];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSTextFinderCaseInsensitiveKey,[NSNumber numberWithInteger:NSTextFinderMatchingTypeContains],NSTextFinderMatchingTypeKey,nil];
[pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions];

// put the new search string in the find bar
[textFinder cancelFindindicator];
[textFinder performAction:NSTextFinderActionSetSearchString];
[textFinder performAction:NSTextFinderActionShowFindInterface]; // make sure the find bar is showing

但是有一个问题.在该代码之后,查找栏中的实际文本字段不会更新.我发现如果我切换第一个响应者然后我可以让它更新…

[myWindow makeFirstResponder:outlineView];
[myWindow makeFirstResponder:textView];

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐