Reputation: 1137
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?
Upvotes: 10
Views: 19958
Reputation: 9590
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)
}
}
}
Upvotes: 1
Reputation: 16792
When you've defined outletLeaderboard, make sure its of UIButton type.
var outletLeaderboard : UIButton = UIButton()
outletLeaderboard.titleLabel!.font = UIFont(name: "Consolas", size: 30)
Upvotes: 0
Reputation: 9346
Use titleLabel
instead of .font
outletLeaderboard.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 20)
Upvotes: 16
Reputation: 1479
Simply access the title label of the button. For example:
button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)
Upvotes: 2
Reputation: 2520
Try
outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
Upvotes: 3