[英]How to change Button text size in iOS 8 swift
I have got a button with a layout outletLeaderboard
. 我有一个带布局
outletLeaderboard
的按钮。 I want to change his text size by the code, so this is what I wrote: 我想通过代码更改文本大小,所以这就是我写的:
outletLeaderboard.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
and then I get an error: 然后我收到一个错误:
'font' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift
'font'不可用:从iOS 7及更早版本开始不推荐使用的API在Swift中不可用
What is the correct line I need to write? 我需要写的正确的行是什么?
使用titleLabel
而不是.font
outletLeaderboard.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 20)
尝试
outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
Simply access the title label of the button. 只需访问按钮的标题标签即可。 For example:
例如:
button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)
With Swift 3: 使用Swift 3:
if let titleLabel = outletLeaderboard.titleLabel {
titleLabel.font = UIFont(name: titleLabel.font.fontName, size: 16)
}
or even better an extension: 甚至更好的扩展:
extension UIButton {
func set(fontSize: CGFloat) {
if let titleLabel = titleLabel {
titleLabel.font = UIFont(name: titleLabel.font.fontName, size: fontSize)
}
}
}
When you've defined outletLeaderboard, make sure its of UIButton type. 当您定义了outletLeaderboard时,请确保其为UIButton类型。
var outletLeaderboard : UIButton = UIButton()
outletLeaderboard.titleLabel!.font = UIFont(name: "Consolas", size: 30)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.