How can I get NSTextFinder?

I have a mac cocoa application with a webview containing some text. I would like to find this text using the default search bar provided by NSTextFinder. As easy as it may seem, having read the link to NSTextFinder, I cannot get the search bar to appear. What am I missing?

As a support:
- Yes, I tried to set findBarContainer to a different view, the same thing. I went back to scroll view to eliminate debugging complexity
- executeTextFinderAction is called to perform a search operation

**App Delegate:** - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { self.textFinderController = [[NSTextFinder alloc] init]; self.webView = [[STEWebView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 200)]; [[self.window contentView] addSubview:self.webView]; [self.textFinderController setClient:self.webView]; [self.textFinderController setFindBarContainer:self.webView.enclosingScrollView]; [[self.webView mainFrame] loadHTMLString:@"sample string" baseURL:NULL]; } - (IBAction)performTextFinderAction:(id)sender { [self.textFinderController performAction:[sender tag]]; } **STEWebView** @interface STEWebView : WebView <NSTextFinderClient> @end @implementation STEWebView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. } - (NSUInteger) stringLength { return [[self stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"] length]; } - (NSString *)string { return [self stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"]; } 
+5
objective-c cocoa macos
source share
3 answers

In my tests, WebView.enclosingScrollView was empty.

 // [self.textFinderController setFindBarContainer:self.webView.enclosingScrollView]; NSLog(@"%@", self.webView.enclosingScrollView); 

Using the following category in NSView , you can find a NSScrollView that extends NSScrollView and sets it as a container, allowing NSTextFinder display nicely within the WebView

 @interface NSView (ScrollView) - (NSScrollView *) scrollView; @end @implementation NSView (ScrollView) - (NSScrollView *) scrollView { if ([self isKindOfClass:[NSScrollView class]]) { return (NSScrollView *)self; } if ([self.subviews count] == 0) { return nil; } for (NSView *subview in self.subviews) { NSView *scrollView = [subview scrollView]; if (scrollView != nil) { return (NSScrollView *)scrollView; } } return nil; } @end 

And in your applicationDidFinishLaunching:aNotification :

 [self.textFinderController setFindBarContainer:[self scrollView]]; 
+2
source share

To display the search bar (as opposed to the default search bar), you just need to use the setUsesFindBar: method.

In your case, you will want to do (in your applicationDidFinishLaunching:aNotification ):

 [textFinderController setUsesFindBar:YES]; //Optionally, incremental searching is a nice feature [textFinderController setIncrementalSearchingEnabled:YES]; 
+1
source share

It finally appeared.

First, install the NSTextFinder instances client in a class that implements the <NSTextFinderClient> protocol:

 self.textFinder.client = self.textFinderController; 

Then, make sure your NSTextFinder has findBarContainer set to the webView category described by Michael Robinson, or view the scroll in the WebView yourself:

 self.textFinder.findBarContainer = [self.webView scrollView]; 

Set the position of the search bar above the content (or anywhere):

 [self.webView scrollView].findBarPosition = NSScrollViewFindBarPositionAboveContent; 

Finally, tell him to appear:

 [self.textFinder performAction:NSTextFinderActionShowFindInterface]; 

It should appear in your web browser:

Also, not sure if this matters, but I have an NSTextFinder in XIB with the link:

 @property (strong) IBOutlet NSTextFinder *textFinder; 

You can also get it by simply executing it as usual: self.textFinder = [[NSTextFinder alloc] init];

+1
source share

Source: https://habr.com/ru/post/650703/


All Articles