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.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
What is the correct line I need to write?
Use titleLabel
instead of .font
outletLeaderboard.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 20)
Simply access the title label of the button. For example:
button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)
With 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)
}
}
}
Try
outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
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