【问题标题】:Setting UILabel text to bold [duplicate]将 UILabel 文本设置为粗体 [重复]
【发布时间】:2014-09-21 10:38:01
【问题描述】:

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

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

【问题讨论】:

  • 查看UILabel 的文档。注意font 属性。

标签: ios swift uilabel


【解决方案1】:

使用UILabelfont 属性:

label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)

或使用默认的system font 加粗文本:

label.font = UIFont.boldSystemFont(ofSize: 16.0)

【讨论】:

  • 我实际上需要第二个选项...非常感谢..
  • 喜欢第二个选项,使用没有自动完成功能的字符串输入字体名称有时会是一个......!干杯芽。
【解决方案2】:

使用属性字符串:

// 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

你需要定义属性。

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 2021-12-31
  • 2016-05-30
  • 2012-12-10
  • 1970-01-01
相关资源
最近更新 更多