NSTextFinder设置搜索字符串并以编程方式清除视觉反馈 [英] NSTextFinder set search string and clear visual feedback programmatically

查看:71
本文介绍了NSTextFinder设置搜索字符串并以编程方式清除视觉反馈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an NSTextView that uses the find bar ([textView setUsesFindBar:YES];).

我有2个问题.

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

  1. How do I clear the visual feedback from a find operation?

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

My problem happens when I programmatically change the content of the textView. The visual feedback for a search operation on the previous content remains after the content change. Obviously these yellow boxes do not apply to the new content so I need a way to clear them when changing the textView content.

注意:我没有实现NSTextFinderClient协议,因为我有一个简单的textView并且查找栏可以正常工作.

Note: I did not implement the NSTextFinderClient protocol because I have a simple textView and the find bar just works without any other effort.

如何将搜索字符串发送到查找栏?

How can I send a search string to the find bar?

推荐答案

我找到了答案,所以对于其他人来说,这是解决方法.

I found my answers, so for others here's how to do it.

首先,您需要一个NSTextFinder实例,以便可以对其进行控制.我们通过代码进行设置.

First you need an instance of NSTextFinder so you can control it. We set that up in code.

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

第一个答案 :要清除视觉反馈,我可以执行以下两项操作之一.我可以取消视觉反馈...

First answer: To clear visual feedback I can do either of 2 things. I can just cancel the visual feedback...

[textFinder cancelFindIndicator];

或者我可以提醒NSTextFinder我将要更改textView内容...

Or I can alert NSTextFinder that I'm about to change my textView content...

[textFinder noteClientStringWillChange];

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

Second answer: There's a global NSFindPboard. You can use that to set a search.

// 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

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

There's a problem though. The actual text field in the find bar does not get updated after that code. I found that if I toggle the first responder then I can get it to update...

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

这篇关于NSTextFinder设置搜索字符串并以编程方式清除视觉反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆