Skip to content

Instantly share code, notes, and snippets.

@mlabraca
Created November 24, 2017 15:31
Show Gist options
  • Save mlabraca/0b1331cd6c1de0907a18105116466596 to your computer and use it in GitHub Desktop.
Save mlabraca/0b1331cd6c1de0907a18105116466596 to your computer and use it in GitHub Desktop.
Extension to apply bold attribute to a text. Seen on [stackoverflow|https://stackoverflow.com/a/37992022/1211664]
extension NSMutableAttributedString {
@discardableResult func bold(_ text: String) -> NSMutableAttributedString {
let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: "AvenirNext-Medium", size: 12)!]
let boldString = NSMutableAttributedString(string:text, attributes: attrs)
append(boldString)
return self
}
@discardableResult func normal(_ text: String) -> NSMutableAttributedString {
let normal = NSAttributedString(string: text)
append(normal)
return self
}
}
let formattedString = NSMutableAttributedString()
formattedString
.bold("Bold Text")
.normal(" Normal Text ")
.bold("Bold Text")
let lbl = UILabel()
lbl.attributedText = formattedString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment