如何更改 Swift 中 UILabel 文本某些部分的字体属性?

How do I change the font attributes of certain sections of a UILabel text in Swift?

示例:

let dummy = UILabel()
dummy.text = "this is a String"

我希望视图显示这样的内容。另外,是否可以在虚拟中更改 "String" 的字体大小?

this is a String

您可以使用 NSAttributedString 来做到这一点:

let dummy = UILabel()
let theString = "this is a String"
let attributedString = NSMutableAttributedString(string: theString, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(24.0)]

attributedString.addAttributes(boldFontAttribute, range: NSMakeRange(10, theString.characters.count))

dummy.attributedText = attributedString