[ios] How to change font of UIButton with Swift

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?

This question is related to ios uibutton swift uifont

The answer is


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)

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.)


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)


Dot-notation is awesome (swift 4.2)

btn.titleLabel?.font = .systemFont(ofSize: 12)

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))


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)

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

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

For Swift 3.0:

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

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


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"

Take a look here.

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

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

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.


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.


You should go through the titleLabel property.

button.titleLabel.font

The font property has been deprecated since iOS 3.0.


This works in Swift 3.0:

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

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)

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to uibutton

How to make a simple rounded button in Storyboard? How to set the title text color of UIButton? How can I make a button have a rounded border in Swift? How to change UIButton image in Swift Changing text of UIButton programmatically swift Disable a Button How to change font of UIButton with Swift Attach parameter to button.addTarget action in Swift Change button background color using swift language Make a UIButton programmatically in Swift

Examples related to swift

Make a VStack fill the width of the screen in SwiftUI Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code in Xcode 10 Convert Json string to Json object in Swift 4 iOS Swift - Get the Current Local Time and Date Timestamp Xcode 9 Swift Language Version (SWIFT_VERSION) How do I use Safe Area Layout programmatically? How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator Safe Area of Xcode 9 The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

Examples related to uifont

How to change font of UIButton with Swift How do you stylize a font in Swift? How do I set bold and italic on UILabel of iPhone/iPad?