我试图改变一个UIButton的字体使用Swift…
myButton.font = UIFont(name: "...", 10)
然而.font已弃用,我不确定如何改变字体,否则。
有什么建议吗?
我试图改变一个UIButton的字体使用Swift…
myButton.font = UIFont(name: "...", 10)
然而.font已弃用,我不确定如何改变字体,否则。
有什么建议吗?
当前回答
正如很多人在这里提到的,你可以设置字体如下:
button.titleLabel?.font = .systemFont(ofSize: 19.0, weight: .bold)
只要确保你的按钮有默认的样式就可以了,否则上面的内容会被系统忽略。
其他回答
您应该遍历titleLabel属性。
button.titleLabel.font
自iOS 3.0以来,字体属性已弃用。
对于Swift 3.0:
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
其中“boldSystemFont”和“16”可以替换为您的自定义字体和大小。
如果你将AttributedString设置为UIButton,那么你可以做下面的事情。
let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
okayButton.setAttributedTitle(attributedText, for: .normal)
正如很多人在这里提到的,你可以设置字体如下:
button.titleLabel?.font = .systemFont(ofSize: 19.0, weight: .bold)
只要确保你的按钮有默认的样式就可以了,否则上面的内容会被系统忽略。
这适用于Swift 3.0:
btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20)