答案 0 :(得分: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)];
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))
答案 1 :(得分:6)
从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;