将UILabel文本设置为粗体

时间:2014-07-29 22:07:09

标签: ios swift uilabel

如何以编程方式在Swift中将UILabel的文本设置为粗体? 到目前为止我的代码:

    var label = UILabel(frame:theFrame)
    label.text = "Foo"

2 个答案:

答案 0 :(得分:188)

使用font的{​​{1}}属性

UILabel

或使用默认label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0) 加粗文字

system font

Swift 3.0

label.font = UIFont.boldSystemFontOfSize(16.0)

答案 1 :(得分:10)

使用属性字符串:

// Define attributes
let labelFont = UIFont(name: "HelveticaNeue-Bold", size: 18)
let attributes :Dictionary = [NSFontAttributeName : labelFont]

// Create attributed string
var attrString = NSAttributedString(string: "Foo", attributes:attributes)
label.attributedText = attrString

您需要定义属性。

使用属性字符串,您可以在一个文本中混合颜色,大小,字体等

相关问题