Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Button text size in iOS 8 swift

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?

like image 484
Eliko Avatar asked Aug 10 '15 14:08

Eliko


4 Answers

Use titleLabel instead of .font

outletLeaderboard.titleLabel!.font =  UIFont(name: "HelveticaNeue-Thin", size: 20)
like image 180
iAnurag Avatar answered Oct 14 '22 01:10

iAnurag


Simply access the title label of the button. For example:

button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)
like image 35
Bierbarbar Avatar answered Oct 14 '22 02:10

Bierbarbar


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)
        }
    }
}
like image 41
Sunkas Avatar answered Oct 14 '22 01:10

Sunkas


Try

outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)
like image 40
Sujay Avatar answered Oct 14 '22 03:10

Sujay