Multiple Font, Style for UILable Text using NSAttributed String.
Published in
2 min readJun 6, 2018
Hey Folks,
As sometimes writing code, we stuck at situation like, some bold & normal text at single phrase, eg: “Hello, Code is Working.” or some cases like
so what / how to do:
- Create multiple labels, assigning different font style & other property, alligning them together, A big NO.
- Intiate NSMutableAttributedString , with multiple style property & assign to label.attributed text. YES !
Implementation.
- Initiate NSMutableAttributedString(string: “”, attributes: []) object.
- string: Assign the string value, say “Hello, I am”.
- attributes: Intakes the array of key value pair, eg : [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17)], defines the font to be default system font & font size with 17 pts.
let attributedText = NSMutableAttributedString(string: “Hello, I am”, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17)])
- Now append attributedText.
attributedText.append(NSAttributedString(string: “ Attributed Text,”, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17), NSAttributedStringKey.foregroundColor: UIColor.blue]))
- Assign to label
<label>.attributedText = attributedText
Adding more properties like:
For above, refer to the code samples at following link.
https://github.com/shubham14896/attributed-text
Thankyou.