How to change Button text size in iOS 8 swift
19,575
Solution 1
Use titleLabel
instead of .font
outletLeaderboard.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 20)
Solution 2
Try
outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
Solution 3
Simply access the title label of the button. For example:
button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)
Solution 4
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)
}
}
}
Author by
Eliko
Updated on June 17, 2022Comments
-
Eliko almost 2 years
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?
-
Eliko over 8 yearsMy font name is costumed: Helvetica Neue Thin. How do I write it in the code line?
-
Eliko over 8 yearsI still get that error: 'font' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift
-
Eliko over 8 yearsWhat if my font name is costumed: Helvetica Neue Thin. How do I write it in the code line?
-
Bierbarbar over 8 yearsYour Button already have this costumed font ? Then try : ` button.titleLabel?.font = UIFont(descriptor:(button.titleLabel?.font.fontDescriptor())!, size: 15) `