首页 > 解决方案 > NSTextView backgroundColor 属性

问题描述

我无法在我的 NSTextView 上使用背景颜色。我使用以下属性:

myTextView.typingAttributes = [
    NSAttributedString.Key.font: NSFont(name: "Helvetica", size: 16.0)!,
    NSAttributedString.Key.foregroundColor: NSColor.textColor,
    NSAttributedString.Key.kern: 2,
] as [NSAttributedString.Key: Any]

myTextView.selectedTextAttributes = [NSAttributedString.Key.foregroundColor: NSColor.systemRed,
                           NSAttributedString.Key.backgroundColor: NSColor.systemYellow]

如上所示,文本正确显示,当我选择文本时,字体按预期变为红色。但是文本的背景颜色不会变为黄色。它发生在暗模式和亮模式(Mojave)中。

我在这里想念什么?

编辑

我找到了造成这种情况的原因。我正在使用NSLayoutManager我覆盖的自定义drawBackground(forGlyphRange glyphsToShow: NSRange, at _: NSPoint)。但是,我没有调用它的 super,因此取消了 backgroundColor 属性。

标签: swiftmacossyntax-highlightingbackground-colornstextview

解决方案


推荐阅读