简体   繁体   English

Swift-从NSTextView中的选定文本中获取颜色

[英]Swift - Get color from selected text in NSTextView

I want to check whether or no a range of a selected text is coloured in Red in my NSTextView. 我想检查一下我的NSTextView中选定文本的范围是否为红色。 I know that it conforms to NSText class and there is a textColor property in NSText but I can't get the color for my NSRange selection. 我知道它符合NSText类,并且NSText中有一个textColor属性,但是我无法为我的NSRange选择获取颜色。

Any ideas? 有任何想法吗?

Maybe you could use the property selectedTextAttributes of NSTextView ( documented here ) 也许您可以使用NSTextView的属性selectedTextAttributes在此处记录

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

Hope that helps. 希望能有所帮助。

Update 更新

As @willeke points out in the comment below my "answer here" won't do the trick. 正如@willeke在下面的评论中指出的那样,我的“此处答案”将无法解决问题。 Have a look at his comment to the original question instead. 看看他对原始问题的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM