Kegham K.
Kegham K.

Reputation: 1614

Check the foreground color of an NSAttributed string

I have an NSAttributedString set in my UITextView and i need to check its NSAttributedStringKey.foregroundColor value.

char = attributedText.attributedSubstring(from: attributedRange)

I tried to use this filter but i really don't know how to use it.

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

Upvotes: 1

Views: 468

Answers (1)

rmaddy
rmaddy

Reputation: 318784

It would be easier to use the attribute method instead of 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
}

Upvotes: 6

Related Questions