Reputation: 19929
I have a UILabel
with an NSAttributedString
with the following code:
// just creating from a notification.mainCopy!
var myMutableString = NSMutableAttributedString();
myMutableString = NSMutableAttributedString(string: notification.mainCopy!, attributes: [NSFontAttributeName:UIFont(name: "Helvetica", size: 14.0)!])
// a notification will have a blueMainCopy1 - just a test so no this could be done better
// How to make the blueMainCopy1 blue?
var myItalicizedRangeBlue = (notification.mainCopy! as NSString).rangeOfString(notification.blueMainCopy1!)
let blueFont = UIFont(name: "HelveticaNeue-BoldItalic", size:14.0) // how to make blue
myMutableString.addAttribute(NSFontAttributeName, value: blueFont!, range: myItalicizedRangeBlue)
How would I add information to blueFont to make it blue in this case?
Is there some way to add:
let blueAttrs = [NSForegroundColorAttributeName : UIColor.blueColor()]
or something?
Upvotes: 1
Views: 121
Reputation: 534893
You already have this:
let blueFont = UIFont(name: "HelveticaNeue-BoldItalic", size:14.0)
myMutableString.addAttribute(
NSFontAttributeName, value: blueFont!,
range: myItalicizedRangeBlue)
And this:
let blueAttrs = [NSForegroundColorAttributeName : UIColor.blueColor()]
So now just add another attribute:
myMutableString.addAttributes(
blueAttrs, range: myItalicizedRangeBlue) // or any desired range
Or, combine the attributes first and add them together, if they have the same range:
let blueFont = UIFont(name: "HelveticaNeue-BoldItalic", size:14.0)
let blueAttrs = [
NSFontAttributeName : blueFont!,
NSForegroundColorAttributeName : UIColor.blueColor()
]
myMutableString.addAttributes(
blueAttrs, range: myItalicizedRangeBlue)
Upvotes: 4