检查NSAttributed字符串的前景色

时间:2017-12-11 21:04:22

标签: ios swift uitextview nsattributedstring

我在NSAttributedString设置了UITextView,我需要检查其NSAttributedStringKey.foregroundColor值。

char = attributedText.attributedSubstring(from: attributedRange)

我尝试使用此过滤器,但我真的不知道如何使用它。

let attr = char.attributes(at: 0, effectiveRange: nil)
attr.filter({ (<#(key: NSAttributedStringKey, value: Any)#>) -> Bool in
            //if true...
        })

1 个答案:

答案 0 :(得分:2)

使用attribute方法而不是attributes

会更容易
char = attributedText.attributedSubstring(from: attributedRange)
if let color = char.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor {
    // color is the foreground color at location 0
} else {
    // there is no foreground color at location 0
}
相关问题