テキストを含む webview を持つ 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"];
}