如何让NSTextFinder显示出来

Luk*_*asz 6 macos cocoa objective-c

我有一个mac cocoa应用程序,其中包含一些包含一些文本的webview.我想使用NSTextFinder提供的默认查找栏搜索该文本.虽然这看起来很容易通过NSTextFinder类引用读取,但我无法显示查找栏.我错过了什么?

作为旁注:
- 是的,我尝试将findBarContainer设置为不同的视图,同样的事情.我回到滚动视图以消除调试的复杂性
- 调用performTextFinderAction来执行查找操作

**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"];
}
Run Code Online (Sandbox Code Playgroud)

var*_*kor 1

要显示查找栏(与默认的查找面板相反),您只需使用该setUsesFindBar:方法即可。

在你的情况下,你会想要做(在你的applicationDidFinishLaunching:aNotification方法中):

[textFinderController setUsesFindBar:YES];
//Optionally, incremental searching is a nice feature
[textFinderController setIncrementalSearchingEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

  • 很抱歉花了很长时间才回复。不幸的是“setUsesFindBar”只是 NSTextView 上的一个方法。 (2认同)