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
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
您可以通过使用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)];
斯威夫特 4 代码:
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))
从 iOS 6 开始,您可以设置UILabel.attributedText
为NSAttributedString
var customString = NSMutableAttributedString(string: "my String");
customString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: NSRange(1...3))
var label = UILabel();
label.attributedText = customString;