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
Setting UILabel text to bold
提问by bhzag
How do you set the text for a UILabel
to bold in Swift programmatically?
My code so far:
如何UILabel
以编程方式在 Swift中将 a 的文本设置为粗体?到目前为止我的代码:
var label = UILabel(frame:theFrame)
label.text = "Foo"
回答by codester
Use font
property of UILabel
:
使用font
属性UILabel
:
label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
or use default system font
to 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
使用属性字符串,您可以在一个文本中混合颜色、大小、字体等