gpt4 book ai didi

macos - 如何以编程方式使用 NSTextFinder?

转载 作者:行者123 更新时间:2023-12-03 17:01:25 26 4
gpt4 key购买 nike

我想在 NSTextView 中执行“查找”操作,而不使用内置查找栏。如何以编程方式设置搜索字符串并在 TextView 中突出显示结果?

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

FWIW,这不是这个问题的重复:NSTextFinder set search string and clear visual feedback programatically

这个问题是关于以编程方式控制查找栏 UI,清除以前的查找结果或填充查找栏搜索字段。

这个问题是关于以编程方式调用“查找”操作,而不使用查找栏 UI。

最佳答案

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

@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

关于macos - 如何以编程方式使用 NSTextFinder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46817490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com