我有一个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"];
}
我在搜索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