Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created January 6, 2015 10:01
Show Gist options
  • Save cemolcay/e76ec9334574d2af595a to your computer and use it in GitHub Desktop.
Save cemolcay/e76ec9334574d2af595a to your computer and use it in GitHub Desktop.
extension UILabel {
func addAttributedString (text: String, color: UIColor, font: UIFont) {
var string = self.text == nil ? "" : self.text!
let newString = string + text
let range = NSRange(location: countElements(string), length: countElements(text))
var att: NSMutableAttributedString?
if let a = self.attributedText {
att = NSMutableAttributedString (attributedString: a)
att?.appendAttributedString(NSAttributedString (string: text))
} else {
att = NSMutableAttributedString (string: newString)
}
att!.addAttribute(NSFontAttributeName, value: font, range: range)
att!.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
self.attributedText = NSAttributedString (attributedString: att!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment