【问题标题】:Swift - Get color from selected text in NSTextViewSwift - 从 NSTextView 中的选定文本中获取颜色
【发布时间】:2017-10-22 13:17:03
【问题描述】:

我想在我的 NSTextView 中检查选定文本的范围是否以红色显示。我知道它符合 NSText 类,并且 NSText 中有一个 textColor 属性,但我无法获得我的 NSRange 选择的颜色。

有什么想法吗?

【问题讨论】:

标签: swift text colors selection nstextview


【解决方案1】:

也许您可以使用NSTextView 的属性selectedTextAttributes (documented here)

let selectedTextAttributes = textview.selectedTextAttributes
if let foregroundColor = selectedTextAttributes[NSForegroundColorAttributeName] as? NSColor {
    //Here is your foreground color
}

希望对您有所帮助。

更新

正如@willeke 在下面的评论中指出的那样,我的“这里的答案”不会奏效。请查看他对原始问题的评论。

【讨论】:

  • "用于指示选择的属性。"与所选文本的属性不同。
  • @Willeke 感谢您提供的信息,我想我很快就和我谈过了。不过,我会在这里留下答案……这样其他人就不会犯同样的错误。再次感谢您的澄清:)
猜你喜欢
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多