Eliko
Eliko

Reputation: 1137

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?

Upvotes: 10

Views: 19958

Answers (5)

Sunkas
Sunkas

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

DirtyBit
DirtyBit

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

iAnurag
iAnurag

Reputation: 9346

Use titleLabel instead of .font

outletLeaderboard.titleLabel!.font =  UIFont(name: "HelveticaNeue-Thin", size: 20)

Upvotes: 16

Bierbarbar
Bierbarbar

Reputation: 1479

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

button.titleLabel?.font = UIFont(name: "Arial-MT", size: 15)

Upvotes: 2

Sujay
Sujay

Reputation: 2520

Try

outletLeaderboard.titleLabel?.font = UIFont(name: outletLeaderboard.font.fontName, size: 37)

Upvotes: 3

Related Questions