检查 NSAttributed 字符串的前景色

Keg*_* K. 1 uitextview nsattributedstring ios swift

我有一套NSAttributedStringUITextView我需要检查它的NSAttributedStringKey.foregroundColor价值。

char = attributedText.attributedSubstring(from: attributedRange)
Run Code Online (Sandbox Code Playgroud)

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

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

rma*_*ddy 6

使用该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
}
Run Code Online (Sandbox Code Playgroud)