Reputation: 3183
Is there a way to set part of a UILabel
to be bold and another part italic?
As in
The quick brown fox jumps over the lazy dog.
It doesn't seem I can do this using TTTAttributedLabel
Upvotes: 0
Views: 8309
Reputation: 2342
You can do this by using NSMutableAttributedString
NSMutableAttributedString *strText = [[NSMutableAttributedString alloc] initWithString:@"Setting different for label text"];
[strText addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica-Bold" size:22]
range:NSMakeRange(0, 10)];
[strText addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica-Italic" size:22]
range:NSMakeRange(10, 10)];
Swift 4 code:
var strText = NSMutableAttributedString(string: "Setting different for label text")
strText.addAttribute(.font, value: UIFont(name: "Helvetica-Bold", size: 22)!, range: NSRange(location: 0, length: 10))
strText.addAttribute(.font, value: UIFont(name: "Helvetica-Italic", size: 22)!, range: NSRange(location: 10, length: 10))
Upvotes: 9
Reputation: 10058
Since iOS 6 you can set UILabel.attributedText
to an NSAttributedString
var customString = NSMutableAttributedString(string: "my String");
customString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: NSRange(1...3))
var label = UILabel();
label.attributedText = customString;
Upvotes: 6