【问题标题】:NSTextFinder set search string and clear visual feedback programmaticallyNSTextFinder 以编程方式设置搜索字符串并清除视觉反馈
【发布时间】:2012-11-30 03:04:19
【问题描述】:

我有一个使用查找栏的NSTextView ([textView setUsesFindBar:YES];)。

我有 2 个问题。

  1. 如何清除查找操作的视觉反馈?

    我的问题发生在我以编程方式更改 textView 的内容时。对先前内容的搜索操作的视觉反馈在内容更改后仍然存在。显然这些黄色框不适用于新内容,所以我需要在更改 textView 内容时清除它们。

    注意:我没有实现 NSTextFinderClient 协议,因为我有一个简单的 textView 并且查找栏无需任何其他努力即可正常工作。

  2. 如何将搜索字符串发送到查找栏?

【问题讨论】:

    标签: objective-c macos cocoa


    【解决方案1】:

    我找到了我的答案,所以对于其他人来说,这是如何做到的。

    首先,您需要一个 NSTextFinder 实例,以便您可以控制它。我们在代码中进行了设置。

    textFinder = [[NSTextFinder alloc] init];
    [textFinder setClient:textView];
    [textFinder setFindBarContainer:[textView enclosingScrollView]];
    [textView setUsesFindBar:YES];
    [textView setIncrementalSearchingEnabled:YES];
    

    第一个答案:为了清除视觉反馈,我可以做两件事中的任何一件。我可以取消视觉反馈...

    [textFinder cancelFindIndicator];
    

    或者我可以提醒 NSTextFinder 我即将更改我的 textView 内容...

    [textFinder noteClientStringWillChange];
    

    第二个答案:有一个全球性的 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
    

    虽然有问题。在该代码之后,查找栏中的实际文本字段不会更新。我发现如果我切换第一响应者,那么我可以让它更新......

    [myWindow makeFirstResponder:outlineView];
    [myWindow makeFirstResponder:textView];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2022-01-07
      • 2011-05-02
      • 1970-01-01
      相关资源
      最近更新 更多