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
.
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.
For Swift 3.0:
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
where "boldSystemFont" and "16" can be replaced with your custom font and size.
btn.titleLabel?.font = .systemFont(ofSize: 12)
You don't need to force unwrap the titleLabel to set it.
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.
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.
In Swift 5, you can utilize dot notation for a bit quicker syntax:
myButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
Otherwise, you'll use:
myButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With