2012-05-07 75 views
5

我有一个mac可可应用程序,其中包含一些文本的webview。我想使用NSTextFinder提供的默认查找栏搜索该文本。这很容易看起来像是通过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"]; 
} 
+0

我在搜索webview是否可以与NSTextFinder进行匹配时偶然发现了这个问题。据我所见,这解决了webview中的文本很简单的一个更容易的问题。关于更普遍问题的讨论在http://www.cocoabuilder。com/archive/cocoa/327153-webview-find-panel-bar-implementation.html和http://stackoverflow.com/questions/4601671/how-to-i-highlight-search-results-in-a-webview- like-safari-and-nstextview-showfi – Nickolay

回答

1

为了让查找栏出现(而不是默认的搜索面板),您只需使用setUsesFindBar:方法。

在你的情况,你会想做的事(在你的applicationDidFinishLaunching:aNotification法):

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

sry永久回应。不幸的是,'setUsesFindBar'只是NSTextView的一种方法。 – Lukasz

2

在我的测试,WebView.enclosingScrollView为空。

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

使用上NSView以下类别,也能够找到延伸NSScrollView嵌套子视图中,并设置作为容器,允许NSTextFinder到内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 

精美的显示和在你的applicationDidFinishLaunching:aNotification

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

没有包含滚动视图的原因是因为这是一个UIWebView(iOS)专用功能。 –

+0

当我将findBarContainer设置为WebView的滚动视图时,滚动视图的findBarView仍然为空,并且在将findBarVisible设置为YES时不会显示它。任何想法为什么? – JuJoDi

1

终于明白了这一点。

首先设置你的NSTextFinder实例客户端类实现<NSTextFinderClient>协议:

self.textFinder.client = self.textFinderController; 

接下来,确保您的NSTextFinder有findBarContainer设置为迈克尔·鲁宾逊描述web视图类别,或者在获得滚动视图web视图自己:

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

设置内容上面查找栏的位置(或者你想去的地方):

[self.webView scrollView].findBarPosition = NSScrollViewFindBarPositionAboveContent; 

最后,告诉它显示出来:

[self.textFinder performAction:NSTextFinderActionShowFindInterface]; 

应该在网页视图显示:

而且,不知道这是否有差别,但我有NSTextFinder在厦门国际银行,以引用插座:

@property (strong) IBOutlet NSTextFinder *textFinder; 

您可能还可以通过简单地INITING它像正常的获得它:self.textFinder = [[NSTextFinder alloc] init];