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

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

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

有什么建议吗?


当前回答

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

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)

其他回答

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

var fontSizeButtonBig:Int = 30

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

这种方法现在行不通了:

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

如此:

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

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

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

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

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)

对于Swift 3.0:

button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)

其中“boldSystemFont”和“16”可以替换为您的自定义字体和大小。