xcode 如何在 iOS 8 swift 中更改按钮文本大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31922387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Button text size in iOS 8 swift
提问by Eliko
I have got a button with a layout outletLeaderboard
.
I want to change his text size by the code, so this is what I wrote:
我有一个带有布局的按钮outletLeaderboard
。我想通过代码改变他的文字大小,所以这是我写的:
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
“字体”不可用:从 iOS 7 及更早版本开始弃用的 API 在 Swift 中不可用
What is the correct line I need to write?
我需要写的正确行是什么?
回答by iAnurag
Use titleLabel
instead of .font
使用titleLabel
代替.font
outletLeaderboard.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 20)
回答by Sujay
Try
尝试
outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
回答by Bierbarbar
Simply access the title label of the button. For example:
只需访问按钮的标题标签。例如:
button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)
回答by Sunkas
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)
}
}
}
回答by DirtyBit
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)