Reputation: 11193
I' have a string which i would like to apply two colors to and then insert that string into an UILabel. The string could look like:
var theString = "Kr. 200 \u{00B7} Red bike"
In this string i would like seperate before the \u{00B7}, so that this string below is red.
"\u{00B7} Red bike"
I guess i need to separate it first and then use some kind of attributed text or how can i do this?
let titleString = theString.componentsSeparatedByString("\u{00B7}")
Upvotes: 0
Views: 295
Reputation: 3752
Using attributed String will work just fine. Create an NSMutableAttributedString variable, apply attributes on it. Then assign it to UILabel's attributed text property.
Here is a sample example:
var myString:NSString = "Blue Text Red Text"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: nil)
myMutableString.addAttribute(NSForegroundColorAttributeName, value:UIColor.blueColor(), range: NSRange(location:0,length:9))
myMutableString.addAttribute(NSForegroundColorAttributeName, value:UIColor.redColor(), range: NSRange(location:10,length:7))
yourlabel.attributedText = myMutableString
Upvotes: 2