将 UILabel 的文本的一部分设置为粗体,将另一部分设置为斜体
Set part of a UILabel's text bold and another part italic
有没有办法将 UILabel
的一部分设置为粗体而另一部分设置为斜体?
如
The quick brown fox jumps over the lazy dog.
我似乎无法使用 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)];
Swift 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;
有没有办法将 UILabel
的一部分设置为粗体而另一部分设置为斜体?
如
The quick brown fox jumps over the lazy dog.
我似乎无法使用 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)];
Swift 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;