我有一个带有包含一些文本的 web 视图的 mac cocoa 应用程序。我想使用 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"];
}