UIlabel toggle bold

时间:2016-11-09 06:51:47

标签: swift

I've managed to bold a UILabel using the following line:

self.nameLabel.font = UIFont.boldSystemFont(ofSize: self.nameLabel.font.pointSize)

But I want to put this in an if statement that will make the UIlabel bold is it isn't bold, and unbold if it is.

Is there a way to do such a thing?

1 个答案:

答案 0 :(得分:4)

虽然还有很多其他方法,但我建议您创建一个存储标签是否为粗体的变量。

var isLabelBold = false

我想你可以在按钮的IBAction方法中加入粗体/非粗体标签代码。现在在该方法中,检查isLabelBold是否为真:

if isLabelBold { // if label is bold
    // unbold it!
    self.nameLabel.font = UIFont.systemFont(ofSize: self.nameLabel.font.pointSize)

    isLabelBold = false
} else { // if label is not bold
    // bold it!
    self.nameLabel.font = UIFont.boldSystemFont(ofSize: self.nameLabel.font.pointSize)
}