Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

objective c - NSTextFinder set search string and clear visual feedback programmatically

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...