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

时间:2016-02-02 14:42:21

标签: ios swift uilabel

示例:

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

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

  

这是字符串

1 个答案:

答案 0 :(得分:0)

您可以使用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