将UILabel文本设置为粗体

bhz*_*zag 80 uilabel ios swift

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

    var label = UILabel(frame:theFrame)
    label.text = "Foo"
Run Code Online (Sandbox Code Playgroud)

cod*_*ter 214

使用的font属性UILabel

label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
Run Code Online (Sandbox Code Playgroud)

或使用默认system font为粗体文本

label.font = UIFont.boldSystemFont(ofSize: 16.0)
Run Code Online (Sandbox Code Playgroud)

Swift 3.0

label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
Run Code Online (Sandbox Code Playgroud)

  • 我实际上需要第二个选择。。。 (2认同)

sum*_*666 11

使用属性字符串:

// 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
Run Code Online (Sandbox Code Playgroud)

您需要定义属性.

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

  • 技术上正确,但有点矫枉过正. (6认同)