ios 将 UILabel 文本设置为粗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25025779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:36:08  来源:igfitidea点击:

Setting UILabel text to bold

iosswiftuilabel

提问by bhzag

How do you set the text for a UILabelto bold in Swift programmatically? My code so far:

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

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

回答by codester

Use fontproperty of UILabel:

使用font属性UILabel

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

or use default system fontto bold text:

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

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

回答by sumofighter666

Use attributed string:

使用属性字符串:

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

You need to define attributes.

您需要定义属性。

Using attributed string you can mix colors, sizes, fonts etc within one text

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