我试图改变一个UIButton的字体使用Swift…

myButton.font = UIFont(name: "...", 10)

然而.font已弃用,我不确定如何改变字体,否则。

有什么建议吗?


当前回答

这适用于Swift 3.0:

btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20) 

其他回答

看看这里。

您应该设置按钮的titleLabel的字体。

myButton.titleLabel!.font = UIFont(name: "...", 10)

我们可以像下面这样使用不同类型的系统字体

myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
myButton.titleLabel?.font = UIFont.italicSystemFont(ofSize:UIFont.smallSystemFontSize)
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.buttonFontSize)

和你的自定义字体如下

    myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12)

斯威夫特5

myButton.titleLabel?.font = UIFont(name: "yourCustomFont", size: CGFloat(yourFontSize))

您应该遍历titleLabel属性。

button.titleLabel.font

自iOS 3.0以来,字体属性已弃用。

如果你只需要改变大小(Swift 4.0):

button.titleLabel?.font = button.titleLabel?.font.withSize(12)