reg*_*633 5 macos cocoa objective-c
我有一个NSTextView使用查找栏([textView setUsesFindBar:YES];).
我有两个问题.
如何清除查找操作的视觉反馈?
当我以编程方式更改textView的内容时,我的问题就出现了.在内容改变之后,对先前内容的搜索操作的视觉反馈保持不变.显然这些黄色框不适用于新内容,因此在更改textView内容时我需要一种方法来清除它们.
注意:我没有实现NSTextFinderClient协议,因为我有一个简单的textView,而查找栏只需要工作而无需任何其他工作.
如何将搜索字符串发送到查找栏?
reg*_*633 14
我找到了答案,所以对于其他人来说,这是怎么做的.
首先,您需要一个NSTextFinder实例,以便您可以控制它.我们在代码中设置了它.
textFinder = [[NSTextFinder alloc] init];
[textFinder setClient:textView];
[textFinder setFindBarContainer:[textView enclosingScrollView]];
[textView setUsesFindBar:YES];
[textView setIncrementalSearchingEnabled:YES];
Run Code Online (Sandbox Code Playgroud)
第一个答案:为了清除视觉反馈,我可以做两件事.我可以取消视觉反馈......
[textFinder cancelFindIndicator];
Run Code Online (Sandbox Code Playgroud)
或者我可以提醒NSTextFinder我即将更改我的textView内容...
[textFinder noteClientStringWillChange];
Run Code Online (Sandbox Code Playgroud)
第二个答案:有一个全球的NSFindPboard.您可以使用它来设置搜索.
// change the NSFindPboard NSPasteboardTypeString
NSPasteboard* pBoard = [NSPasteboard pasteboardWithName:NSFindPboard];
[pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, NSPasteboardTypeTextFinderOptions, nil] owner:nil];
[pBoard setString:@"new search" forType:NSStringPboardType];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSTextFinderCaseInsensitiveKey, [NSNumber numberWithInteger:NSTextFinderMatchingTypeContains], NSTextFinderMatchingTypeKey, nil];
[pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions];
// put the new search string in the find bar
[textFinder cancelFindIndicator];
[textFinder performAction:NSTextFinderActionSetSearchString];
[textFinder performAction:NSTextFinderActionShowFindInterface]; // make sure the find bar is showing
Run Code Online (Sandbox Code Playgroud)
但是有一个问题.在该代码之后,查找栏中的实际文本字段不会更新.我发现如果我切换第一个响应者然后我可以让它更新...
[myWindow makeFirstResponder:outlineView];
[myWindow makeFirstResponder:textView];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1005 次 |
| 最近记录: |