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.
- Create the button: Instantiate the
UIButtonand 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
UIFonttobutton.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:
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:
This matters for building accessible iOS apps, because the font size follows the user's device settings.
| Step | Description |
| UIButton Creation | Instantiate and setup the UIButton |
| Access titleLabel | Access titleLabel to alter font properties |
| Set Custom Font | Use UIFont to set a custom or system font |
| Dynamic Type | Consider Accessibility options for adaptive sizing |
| Font Availability | Ensure 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.
Related reading
- How to change fontFamily of TextView in Android
- How to change height of grouped UITableView header?
- How to change line color in EditText
- How to change LocalizedStringKey to String in SwiftUI
- How to change menu item text dynamically in Android
- How to change Navigation Bar color in iOS 7?
- how to change navigationitem title color
- How to change position of Toast in Android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.