Francis F
Francis F

Reputation: 3285

Setting textView.typingAttributes foreground color in Swift 4

I'm using the below method to set my textView.typingAttributes foreground color to red in Swift 3.

textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red

Now, that I've migrated to Swift 4 its showing error, I tried like below but its still showing error. Whats the correct way to set it?

textView.typingAttributes[NSAttributedStringKey.foregroundColor] = UIColor.red

Upvotes: 5

Views: 2071

Answers (2)

sandpat
sandpat

Reputation: 1485

For Swift 4.2 and Swift 5.0, it is

textView.typingAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]

Upvotes: 1

matt
matt

Reputation: 535230

Try this:

textView.typingAttributes[NSAttributedStringKey.foregroundColor.rawValue] =
    UIColor.red

Upvotes: 8

Related Questions