Setting UILabel text to bold [duplicate]

有些话、适合烂在心里 提交于 2020-07-04 05:50:18

问题


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

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

回答1:


Use font property of UILabel:

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

or use default system font to bold text:

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



回答2:


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



来源:https://stackoverflow.com/questions/25025779/setting-uilabel-text-to-bold

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!