NSTextFinder设置搜索字符串和清晰的视觉反馈编程 [英] NSTextFinder set search string and clear visual feedback programatically

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

问题描述

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



我有2个问题。


  1. 从查找操作的视觉反馈?



    我以编程方式更改textView的内容时发生问题。在内容改变之后,对先前内容的搜索操作的视觉反馈保持。显然这些黄色框不适用于新的内容,所以我需要一个方法来改变textView内容时清除它们。



    注意:我没有实现NSTextFinderClient协议,因为我有一个简单的textView和查找栏只是工作没有任何其他努力。


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



解决方案

我找到了我的答案,所以对于其他人来说, >

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

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

第一个答案 :清除视觉反馈我可以做两件事之一。我可以取消视觉反馈...

  [textFinder cancelFindIndicator]; 

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

  [textFinder noteClientStringWillChange]; 

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

  //更改NSFindPboard NSPasteboardTypeString 
NSPasteboard * pBoard = [NSPasteboard pasteboardWithName :NSFindPboard];
[pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString,NSPasteboardTypeTextFinderOptions,nil] owner:nil];
[pBoard setString:@new searchforType:NSStringPboardType];
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSTextFinderCaseInsensitiveKey,[NSNumber numberWithInteger:NSTextFinderMatchingTypeContains],NSTextFinderMatchingTypeKey,nil];
[pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions];

//将新的搜索字符串放在查找栏中
[textFinder cancelFindIndicator];
[textFinder performAction:NSTextFinderActionSetSearchString];
[textFinder performAction:NSTextFinderActionShowFindInterface]; //确保查找栏显示

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

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


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

I have 2 questions.

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

    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.

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

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

解决方案

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

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];

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

[textFinder noteClientStringWillChange];

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天全站免登陆