Created
November 24, 2017 15:31
-
-
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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