objective-c – 如何在后台窗口中设置[NSTextView selectedTextAttributes]?

objective-c – 如何在后台窗口中设置[NSTextView selectedTextAttributes]?,第1张

概述[NSTextView selectedTextAttributes]的默认值在我的应用程序中无法使用,因为我允许用户选择与背景颜色几乎完全相同的颜色(语法高亮). 我已经写了一些数学来确定合适的颜色,可以使用它来设置它: textView.selectedTextAttributes = @{ NSBackgroundColorAttributeName: [NSColor yellowCo [NSTextVIEw selectedTextAttributes]的默认值在我的应用程序中无法使用,因为我允许用户选择与背景颜色几乎完全相同的颜色(语法高亮).

我已经写了一些数学来确定合适的颜色,可以使用它来设置它:

textVIEw.selectedTextAttributes = @{  NSBackgroundcolorAttributename: [NScolor yellowcolor],NSForegroundcolorAttributename: [NScolor redcolor]  };

但是当窗口在后台时,它仍然使用系统默认的浅灰色.

我已经在活动与非活动窗口中附加了上述代码的屏幕截图. – 如何更改非活动窗口的选定文本背景颜色?

解决方法 您可以通过覆盖NSLayoutManager的绘图方法来覆盖颜色.

final class LayoutManager1: NSLayoutManager {    overrIDe func fillBackgroundRectArray(rectArray: UnsafePointer<NSRect>,count rectCount: Int,forCharacterRange charRange: NSRange,color: NScolor) {        let color1 = color == NScolor.secondarySelectedControlcolor() ? NScolor.redcolor() : color        color1.setFill()        super.fillBackgroundRectArray(rectArray,count: rectCount,forCharacterRange: charRange,color: color1)        color.setFill()    }}

并将NSTextVIEw的布局管理器替换为它.

textVIEw.textContainer!.replaceLayoutManager(layoutManager1)

这是full working example.

当@Kyle询问setFill的原因时,我添加了一些更新.

来自Apple手册:

… the charRange and color parameters are passed in merely for informational purposes; the color is
already set in the graphics state. If for any reason you modify it,you must restore it before
returning from this method. …

这意味着将其他颜色传入超级调用无效,您只需要NScolor.setFill使它与超级调用一起工作.此外,手册需要将其设置回原始版本.

总结

以上是内存溢出为你收集整理的objective-c – 如何在后台窗口中设置[NSTextView selectedTextAttributes]?全部内容,希望文章能够帮你解决objective-c – 如何在后台窗口中设置[NSTextView selectedTextAttributes]?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1024349.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存