0

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

4

2 回答 2

9

您可以通过使用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))
于 2015-08-24T06:44:18.330 回答
6

从 iOS 6 开始,您可以设置UILabel.attributedTextNSAttributedString

var customString = NSMutableAttributedString(string: "my String");

customString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: NSRange(1...3))

var label  = UILabel();
label.attributedText = customString;
于 2015-08-24T06:46:45.853 回答