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

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

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

有什么建议吗?


当前回答

如果你将AttributedString设置为UIButton,那么你可以做下面的事情。

let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
okayButton.setAttributedTitle(attributedText, for: .normal)

其他回答

这对我很有用,谢谢。我想改变文本大小,而不是改变字体名称。

var fontSizeButtonBig:Int = 30

btnMenu9.titleLabel ?。font = .systemFont(ofSize: CGFloat(fontSizeButtonBig))

您应该遍历titleLabel属性。

button.titleLabel.font

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

看看这里。

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

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

斯威夫特5

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

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

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)