如何以编程方式使用NSTextFinder? [英] How to use NSTextFinder programmatically?

查看:127
本文介绍了如何以编程方式使用NSTextFinder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NSTextView中执行查找"操作,而不使用内置的查找栏.如何以编程方式设置搜索字符串并使结果在文本视图中突出显示?

I'd like to do a "find" operation in an NSTextView without using the built-in find bar. How can I programmatically set a search string and have the results highlighted inside the text view?

这适用于macOS 10.12及更高版本.

This is for macOS 10.12 and higher.

FWIW,这不是此问题的重复项: NSTextFinder设置搜索字符串并以编程方式清除视觉反馈

FWIW, this is not a duplicate of this question: NSTextFinder set search string and clear visual feedback programatically

该问题是关于以编程方式控制查找栏用户界面,即清除先前的查找结果或填充查找栏搜索字段.

That question is about programmatically controlling the find bar UI, either clearing previous find results or populating the find bar search field.

这个问题是关于以编程方式调用查找"操作而根本不使用查找栏UI.

This question is about invoking a "find" operation programmatically without using the find bar UI at all.

推荐答案

这是我的测试代码(hacky,实验性):

Here's my test code (hacky, experimental):

@interface ViewController ()

@property (strong) NSTextFinder *textFinder;
@property (weak) IBOutlet NSTextView *textView; // find Uses Bar, Incremental Searching is on
@property (weak) NSView *myFindBarView;
@property (weak) IBOutlet NSView *findBarContainerView; // hidden
@property BOOL barVisible;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textFinder = [[NSTextFinder alloc] init];
    self.textFinder.incrementalSearchingEnabled = YES;
    self.textFinder.incrementalSearchingShouldDimContentView = YES;
    self.textFinder.client = (id<NSTextFinderClient>)self.textView;
    self.textFinder.findBarContainer = self;
    [self.textFinder performAction:NSTextFinderActionShowFindInterface];
}

- (IBAction)testSearchString:(id)sender {
    // action method of a Test button, searches for "found"
    [self.textFinder cancelFindIndicator];

    // search the subviews for a view of class NSSearchField
    __block __weak NSSearchField *(^weak_findSearchField)(NSView *);
    NSSearchField *(^findSearchField)(NSView *);
    weak_findSearchField = findSearchField = ^(NSView *view) {
        if ([view isKindOfClass:[NSSearchField class]])
            return (NSSearchField *)view;
        __block NSSearchField *foundView = nil;
        [view.subviews enumerateObjectsUsingBlock:^(NSView *subview, NSUInteger idx, BOOL *stop) {  
            foundView = weak_findSearchField(subview);
            if (foundView)
                *stop = YES;
        }];
        return foundView;
    };

    NSSearchField *searchField = findSearchField(self.myFindBarView);
    [searchField setStringValue:@"found"];
    // execute the action of the search field to confirm the new value and do a search
    [searchField sendAction:searchField.action to:searchField.target];
    /* add to select all
    [self.textFinder performAction:NSTextFinderActionSelectAll];
    */
}

// NSTextFinderBarContainer

- (void)findBarViewDidChangeHeight {
}

- (NSView *)findBarView {
    return self.myFindBarView;
}

- (void)setFindBarView:(NSView *)theView {
    self.myFindBarView = theView;
    if (theView) {
        NSRect frame = theView.frame;
        frame.size.width = self.findBarContainerView.bounds.size.width;
        theView.frame = frame;
        [self.findBarContainerView addSubview:theView];
    }
}

- (NSView *)contentView {
    return self.textView.enclosingScrollView.contentView;
}

- (BOOL)isFindBarVisible {
    return self.barVisible;
}

- (void)setFindBarVisible:(BOOL)theVisible {
    self.barVisible = theVisible;
}

@end

这篇关于如何以编程方式使用NSTextFinder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆