更改 NSTextView 中的文本选择颜色

发布于 2024-10-04 20:49:27 字数 135 浏览 5 评论 0原文

我正在尝试在 NSTextView 上编写“突出显示”功能。目前,一切进展顺利。您选择一个文本范围,该文本的背景颜色将更改为黄色。然而,虽然它仍然处于选中状态,但背景是所选文本的标准蓝色。如何使标准选择指示器颜色在某些情况下不显示?

谢谢!

I'm trying to write a "highlight" feature on an NSTextView. Currently, everything works great. You select a range of text and the background color of that text changes to yellow. However, while it's still selected, the background is that standard blue of selected text. How do I make that standard selection indicator color not show up in certain cases?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-10-11 20:49:27

使用 -[NSTextView setSelectedTextAttributes:...]

例如:

[textView setSelectedTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [NSColor blackColor], NSBackgroundColorAttributeName,
      [NSColor whiteColor], NSForegroundColorAttributeName,
      nil]];

如果您根本不希望以任何方式指示选择(除了隐藏插入点),您可以简单地传递一个空字典。

另一种选择是监视选择更改并使用 临时属性。请注意,临时属性用于显示拼写和语法错误并查找结果;因此,如果您关心保留 NSTextView 的这些功能,请确保仅添加和删除临时属性,而不是替换它们。

一个例子是(在 NSTextView 子类中):

- (void)setSelectedRanges:(NSArray *)ranges affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag;
{
    NSArray *oldRanges = [self selectedRanges];
    for (NSValue *v in oldRanges) {
        NSRange oldRange = [v rangeValue];
        if (oldRange.length > 0)
            [[self layoutManager] removeTemporaryAttribute:NSBackgroundColorAttributeName forCharacterRange:oldRange];
    }

    for (NSValue *v in ranges) {
        NSRange range = [v rangeValue];
        if (range.length > 0)
            [[self layoutManager] addTemporaryAttributes:[NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSBackgroundColorAttributeName]
                                       forCharacterRange:range];
    }

    [super setSelectedRanges:ranges affinity:affinity stillSelecting:stillSelectingFlag];
}

Use -[NSTextView setSelectedTextAttributes:...].

For example:

[textView setSelectedTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [NSColor blackColor], NSBackgroundColorAttributeName,
      [NSColor whiteColor], NSForegroundColorAttributeName,
      nil]];

You can simply pass an empty dictionary if you don't want the selection indicated in any way at all (short of hiding the insertion point).

Another option is to watch for selection changes and apply the "selection" using temporary attributes. Note that temporary attributes are used to show spelling and grammar mistakes and find results; so if you care about preserving these features of NSTextView then make sure only to add and remove temporary attributes, not replace them.

An example of this is (in a NSTextView subclass):

- (void)setSelectedRanges:(NSArray *)ranges affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag;
{
    NSArray *oldRanges = [self selectedRanges];
    for (NSValue *v in oldRanges) {
        NSRange oldRange = [v rangeValue];
        if (oldRange.length > 0)
            [[self layoutManager] removeTemporaryAttribute:NSBackgroundColorAttributeName forCharacterRange:oldRange];
    }

    for (NSValue *v in ranges) {
        NSRange range = [v rangeValue];
        if (range.length > 0)
            [[self layoutManager] addTemporaryAttributes:[NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSBackgroundColorAttributeName]
                                       forCharacterRange:range];
    }

    [super setSelectedRanges:ranges affinity:affinity stillSelecting:stillSelectingFlag];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文