我试图改变一个UIButton的字体使用Swift…
myButton.font = UIFont(name: "...", 10)
然而.font已弃用,我不确定如何改变字体,否则。
有什么建议吗?
我试图改变一个UIButton的字体使用Swift…
myButton.font = UIFont(name: "...", 10)
然而.font已弃用,我不确定如何改变字体,否则。
有什么建议吗?
当前回答
看看这里。
您应该设置按钮的titleLabel的字体。
myButton.titleLabel!.font = UIFont(name: "...", 10)
其他回答
您应该遍历titleLabel属性。
button.titleLabel.font
自iOS 3.0以来,字体属性已弃用。
如果你只需要改变大小(Swift 4.0):
button.titleLabel?.font = button.titleLabel?.font.withSize(12)
如果你有字体大小的问题(你的字体没有响应大小的变化)…
@codester有正确的代码:
myButton.titleLabel!.font = UIFont(name: YourfontName, size: 20)
然而,我的字体大小并没有改变。结果是我要求的字体并不存在(“HelveticaNeue-Regular”)。它并没有导致崩溃,但似乎只是因为它忽略了字体声明。一旦我将字体更改为一些确实存在的东西,更改为“size: x”确实呈现。
使用titleLabel代替。字体属性在iOS 3.0中已弃用。它也不能在Objective-C中工作。titleLabel是用于在UIButton上显示标题的标签。
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
然而,在设置标题文本时,你应该只使用setTitle:forControlState:。不要使用titleLabel直接为title设置任何文本。
我们可以像下面这样使用不同类型的系统字体
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)