Swift
UIButton
iOS Development
Font Customization
Mobile App Development

How to change the font of a UIButton in Swift

Codemia Knowledge Hub ·

Set the font on the button's title label: button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 18). titleLabel is a read-only property that gives you the UILabel behind the button's title, and its font accepts any UIFont, whether a custom font, a system font or a Dynamic Type style.

How UIButton fonts work

UIButton lets you customize its title text, including font, color and style. The font itself is a UIFont, which holds the properties iOS needs to render text. The button does not take a font directly; you reach the title's UILabel through titleLabel and change the font there. Changing the font is a quick way to emphasize a call-to-action button or to keep custom brand typography consistent.

Setting a custom font

The steps are: access the button's titleLabel, then set its font to a new UIFont object.

swift
1import UIKit
2
3class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6        
7        // Create a UIButton
8        let button = UIButton(type: .system)
9        button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
10        button.setTitle("Press Me", for: .normal)
11        
12        // Customizing the font
13        button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 18)
14        
15        // Adding the button to the view
16        self.view.addSubview(button)
17    }
18}
  • Create the button: Instantiate the UIButton and set its frame and title.
  • Choose the font: UIFont(name:size:) picks a font by name. Apple ships many fonts, and you can add your own to the project.
  • Apply it: Assign the UIFont to button.titleLabel?.font.

A custom font only works if it is available: include the font file in the project bundle and register it in the Info.plist file.

Using the system font

Without a custom font, use UIFont's system font initializer:

swift
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .bold)

This is recommended for consistency across device types and screen resolutions.

Supporting Dynamic Type

To respect the text size the user picks in Settings, use preferredFont(forTextStyle:) and let the label adjust when that size changes:

swift
button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
button.titleLabel?.adjustsFontForContentSizeCategory = true

This matters for building accessible iOS apps, because the font size follows the user's device settings.

StepDescription
UIButton CreationInstantiate and setup the UIButton
Access titleLabelAccess titleLabel to alter font properties
Set Custom FontUse UIFont to set a custom or system font
Dynamic TypeConsider Accessibility options for adaptive sizing
Font AvailabilityEnsure custom fonts are added to the project bundle

Whatever font you choose, keep the style consistent across the app so the interface has a cohesive look and feel. Considering accessibility from the start, through Dynamic Type, gives every user a better experience.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms