ios 如何使用 Swift 更改 UIButton 的字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25002017/
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 font of UIButton with Swift
提问by Stephen Fox
I am trying to change the font of a UIButton using Swift...
我正在尝试使用 Swift 更改 UIButton 的字体...
myButton.font = UIFont(name: "...", 10)
However .font
is deprecated and I'm not sure how to change the font otherwise.
但是.font
已弃用,我不知道如何更改字体。
Any suggestions?
有什么建议?
回答by codester
Use titleLabel
instead. The font
property is deprecated in iOS 3.0. It also does not work in Objective-C. titleLabel
is label used for showing title on UIButton
.
使用titleLabel
来代替。该font
属性在 iOS 3.0 中已弃用。它也不适用于 Objective-C。titleLabel
是用于在 上显示标题的标签UIButton
。
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
However, while setting title text you should only use setTitle:forControlState:
. Do not use titleLabel
to set any text for title directly.
但是,在设置标题文本时,您应该只使用setTitle:forControlState:
. 不要titleLabel
用于直接为标题设置任何文本。
回答by ruthless_g
For Swift 3.0:
对于Swift 3.0:
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
where "boldSystemFont" and "16" can be replaced with your custom font and size.
其中“boldSystemFont”和“16”可以替换为您的自定义字体和大小。
回答by eonist
Dot-notation is awesome (swift 4.2)
点符号很棒(swift 4.2)
btn.titleLabel?.font = .systemFont(ofSize: 12)
回答by teradyl
You don't need to force unwrap the titleLabel to set it.
您不需要强制打开 titleLabel 来设置它。
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
Since you're not using the titleLabel here, you can just optionally use it and if it's nil it will just be a no-op.
由于您没有在此处使用 titleLabel,因此您可以选择使用它,如果它为零,它将只是一个空操作。
I'll also add as other people are saying, the font property is deprecated, and make sure to use setTitle:forControlState:
when setting the title text.
我还要补充一点,正如其他人所说,不推荐使用字体属性,并确保setTitle:forControlState:
在设置标题文本时使用。
回答by zneak
From the documentation:
从文档:
The font used to display text on the button. (Deprecated in iOS 3.0. Use the
font
property of thetitleLabel
instead.)
用于在按钮上显示文本的字体。(在 iOS 3.0 中已弃用。改用 的
font
属性titleLabel
。)
回答by Dave G
If you're having font size issues (your font isn't responding to size changes)...
如果您遇到字体大小问题(您的字体没有响应大小更改)...
@codester has the right code:
@codester 有正确的代码:
myButton.titleLabel!.font = UIFont(name: YourfontName, size: 20)
However, my font size wasn't changing. It turns out that I asking for a font that didn't exist ("HelveticaNeue-Regular"). It wasn't causing a crash, but seemed to be just ignoring that font statement because of it. Once I changed the font to something that does exist, changes to "size: x" did render.
但是,我的字体大小没有改变。事实证明,我要求使用一种不存在的字体(“HelveticaNeue-Regular”)。它没有导致崩溃,但似乎只是因为它而忽略了该字体声明。一旦我将字体更改为确实存在的字体,对“大小:x”的更改就会呈现。
回答by Sultan Ali
we can use different types of system fonts like below
我们可以使用不同类型的系统字体,如下所示
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
myButton.titleLabel?.font = UIFont.italicSystemFont(ofSize:UIFont.smallSystemFontSize)
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.buttonFontSize)
and your custom font like below
和您的自定义字体,如下所示
myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12)
回答by Anirudha Mahale
If you are setting AttributedString to the UIButton then you can do the below thing.
如果您将 AttributedString 设置为 UIButton,那么您可以执行以下操作。
let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
okayButton.setAttributedTitle(attributedText, for: .normal)
回答by Karbaman
If you need to change only size (Swift 4.0):
如果您只需要更改大小(Swift 4.0):
button.titleLabel?.font = button.titleLabel?.font.withSize(12)
回答by ram
This works in Swift 3.0:
这适用于 Swift 3.0:
btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20)