[英]How to change font of UIButton with Swift
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?有什么建议么?
Use titleLabel
instead.请改用
titleLabel
。 The font
property is deprecated in iOS 3.0. font
属性在 iOS 3.0 中已弃用。 It also does not work in Objective-C.它也不适用于 Objective-C。
titleLabel
is label used for showing title on UIButton
. titleLabel
是用于在UIButton
上显示标题的标签。
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
However, while setting title text you should only use setTitle:forControlState:
.但是,在设置标题文本时,您应该只使用
setTitle:forControlState:
。 Do not use titleLabel
to set any text for title directly.不要使用
titleLabel
直接为标题设置任何文本。
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”可以替换为您的自定义字体和大小。
As mentioned by many here you can set the font with something like:正如这里许多人所提到的,您可以使用以下内容设置字体:
button.titleLabel?.font = .systemFont(ofSize: 19.0, weight: .bold)
Just make sure your button has default
style though for this to be applicable, otherwise the above gets ignored by the system.只需确保您的按钮具有
default
样式即可适用,否则系统会忽略上述样式。
btn.titleLabel?.font = .systemFont(ofSize: 12)
You don't need to force unwrap the titleLabel to set it.您不需要强制打开 titleLabel 来设置它。
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.我还将添加其他人所说的,不推荐使用 font 属性,并确保在设置标题文本时使用
setTitle:forControlState:
。
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 中已弃用。请改用
titleLabel
的font
属性。)
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").
事实证明,我要求使用一种不存在的字体(“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.
一旦我将字体更改为确实存在的字体,对“大小:x”的更改就会呈现。
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)
In Swift 5, you can utilize dot notation for a bit quicker syntax:在 Swift 5 中,您可以使用点符号来获得更快的语法:
myButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
Otherwise, you'll use:否则,您将使用:
myButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
如果您只需要更改大小( Swift 4.0 ):
button.titleLabel?.font = button.titleLabel?.font.withSize(12)
You should go through the titleLabel
property.您应该查看
titleLabel
属性。
button.titleLabel.font
The font
property has been deprecated since iOS 3.0.自 iOS 3.0 起,
font
属性已被弃用。
这适用于 Swift 3.0:
btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20)
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)
Example: button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
示例:
button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
This way doesn't work now:这种方式现在不起作用:
btn.titleLabel?.font = UIFont(name: "Helvetica", size:12)
This works:这有效:
btn.titleLabel?.font = UIFont.init(name: "Helvetica", size:12)
To do this using storyboard, go to the attributes inspector while your button is selected.要执行此操作,请在选择按钮时使用 storyboard、go 到属性检查器。 In the third field from the top ("Title") select "Attributed".
在自上而下的第三个字段(“标题”)select“属性”。 This will bring up the font drop-down list where you can easily change the font.
这将调出字体下拉列表,您可以在其中轻松更改字体。
this work for me, thanks.这对我有用,谢谢。 I want change text size only not change font name.
我只想更改文本大小而不更改字体名称。
var fontSizeButtonBig:Int = 30
btnMenu9.titleLabel?.font = .systemFont(ofSize: CGFloat(fontSizeButtonBig)) btnMenu9.titleLabel?.font = .systemFont(ofSize: CGFloat(fontSizeButtonBig))
Swift 5 Swift 5
myButton.titleLabel?.font = UIFont(name: "yourCustomFont", size: CGFloat(yourFontSize))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.