How to change font of UIButton with Swift

IosUibuttonSwiftUifont

Ios Problem Overview


I am trying to change the font of a UIButton using Swift...

myButton.font = UIFont(name: "...", 10)

However .font is deprecated and I'm not sure how to change the font otherwise.

Any suggestions?

Ios Solutions


Solution 1 - Ios

Use titleLabel instead. The font property is deprecated in iOS 3.0. It also does not work in Objective-C. titleLabel is label used for showing title on UIButton.

myButton.titleLabel?.font =  UIFont(name: YourfontName, size: 20)

However, while setting title text you should only use setTitle:forControlState:. Do not use titleLabel to set any text for title directly.

Solution 2 - Ios

For Swift 3.0:

button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)

where "boldSystemFont" and "16" can be replaced with your custom font and size.

Solution 3 - Ios

Dot-notation is awesome 
btn.titleLabel?.font = .systemFont(ofSize: 12)

Solution 4 - Ios

As mentioned by many here you can set the font with something like:

button.titleLabel?.font = .systemFont(ofSize: 19.0, weight: .bold)

Just make sure your button has default style though for this to be applicable, otherwise the above gets ignored by the system.

enter image description here

Solution 5 - Ios

If you need to change only size (Swift 4.0):

button.titleLabel?.font = button.titleLabel?.font.withSize(12)

Solution 6 - Ios

You don't need to force unwrap the titleLabel to set it.

myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)

Since you're not using the titleLabel here, you can just optionally use it and if it's nil it will just be a no-op.

I'll also add as other people are saying, the font property is deprecated, and make sure to use setTitle:forControlState: when setting the title text.

Solution 7 - Ios

In Swift 5, you can utilize dot notation for a bit quicker syntax:

myButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)

Otherwise, you'll use:

myButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)

Solution 8 - Ios

From the documentation:

> The font used to display text on the button. (Deprecated in iOS 3.0. Use the font property of the titleLabel instead.)

Solution 9 - Ios

If you're having font size issues (your font isn't responding to size changes)...

@codester has the right code:

myButton.titleLabel!.font =  UIFont(name: YourfontName, size: 20)

However, my font size wasn't changing. It turns out that I asking for a font that didn't exist ("HelveticaNeue-Regular"). It wasn't causing a crash, but seemed to be just ignoring that font statement because of it. Once I changed the font to something that does exist, changes to "size: x" did render.

Solution 10 - Ios

we can use different types of system fonts like below

myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
myButton.titleLabel?.font = UIFont.italicSystemFont(ofSize:UIFont.smallSystemFontSize)
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.buttonFontSize)

and your custom font like below

    myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12)

Solution 11 - Ios

Take a look here.

You should set the font of the button's titleLabel instead.

myButton.titleLabel!.font = UIFont(name: "...", 10)

Solution 12 - Ios

You should go through the titleLabel property.

button.titleLabel.font

The font property has been deprecated since iOS 3.0.

Solution 13 - Ios

This works in Swift 3.0:

btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20) 

Solution 14 - Ios

If you are setting AttributedString to the UIButton then you can do the below thing.

let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
okayButton.setAttributedTitle(attributedText, for: .normal)

Solution 15 - Ios

Example: button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)

  • If you want to use defaul font from it's own family, use for example: "HelveticaNeue"
  • If you want to specify family font, use for example: "HelveticaNeue-Bold"

Solution 16 - Ios

This way doesn't work now:

 btn.titleLabel?.font = UIFont(name: "Helvetica", size:12)

This works:

 btn.titleLabel?.font = UIFont.init(name: "Helvetica", size:12)

Solution 17 - Ios

this work for me, thanks. I want change text size only not change font name.

var fontSizeButtonBig:Int = 30

btnMenu9.titleLabel?.font = .systemFont(ofSize: CGFloat(fontSizeButtonBig))

Solution 18 - Ios

To do this using storyboard, go to the attributes inspector while your button is selected. In the third field from the top ("Title") select "Attributed". This will bring up the font drop-down list where you can easily change the font.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephen FoxView Question on Stackoverflow
Solution 1 - IoscodesterView Answer on Stackoverflow
Solution 2 - Iosruthless_gView Answer on Stackoverflow
Solution 3 - IosSentry.coView Answer on Stackoverflow
Solution 4 - IosatineoSEView Answer on Stackoverflow
Solution 5 - IosPetr LazarevView Answer on Stackoverflow
Solution 6 - IosteradylView Answer on Stackoverflow
Solution 7 - IosandrewlundyView Answer on Stackoverflow
Solution 8 - IoszneakView Answer on Stackoverflow
Solution 9 - IosDave GView Answer on Stackoverflow
Solution 10 - IosSultan AliView Answer on Stackoverflow
Solution 11 - IosConnorView Answer on Stackoverflow
Solution 12 - IosChris WagnerView Answer on Stackoverflow
Solution 13 - IosramView Answer on Stackoverflow
Solution 14 - IosAnirudha MahaleView Answer on Stackoverflow
Solution 15 - IosjanazView Answer on Stackoverflow
Solution 16 - IosRealNmaeView Answer on Stackoverflow
Solution 17 - IosmahasamView Answer on Stackoverflow
Solution 18 - Ios23shay83View Answer on Stackoverflow