iOS
UILabel
font size
programmatically
Swift programming

iOS set font size of UILabel Programmatically

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing a UILabel font size programmatically is straightforward in UIKit, but the best solution depends on whether you want a fixed size, a weighted system font, a custom font, or dynamic type support. The API for all of these flows through the label’s font property. Good code also considers Auto Layout and accessibility so the label still behaves correctly after the font changes.

Set a Fixed System Font Size

The simplest approach is to assign a system font with the size you want.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let label = UILabel()
8        label.translatesAutoresizingMaskIntoConstraints = false
9        label.text = "Order Confirmed"
10        label.font = UIFont.systemFont(ofSize: 24)
11
12        view.addSubview(label)
13
14        NSLayoutConstraint.activate([
15            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
16            label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
17        ])
18    }
19}

This is the direct answer when the design calls for a specific fixed size.

Use Weight and Style Intentionally

Font size is only one part of the visual result. If the label should be bold or semibold, set that at the same time instead of treating size and weight as unrelated tweaks.

swift
label.font = UIFont.systemFont(ofSize: 20, weight: .semibold)

This is usually clearer than using the default system weight and then trying to fake emphasis elsewhere with color or spacing.

Load a Custom Font Safely

If the app uses a bundled font, use UIFont(name:size:) and handle failure explicitly. That method returns an optional because the font must be registered correctly in the app bundle.

swift
1if let customFont = UIFont(name: "AvenirNext-DemiBold", size: 22) {
2    label.font = customFont
3} else {
4    label.font = UIFont.systemFont(ofSize: 22, weight: .medium)
5}

The fallback matters because a missing font name otherwise leaves the label with no obvious styling fix.

Support Dynamic Type When Text Should Scale

For user-facing content, a fixed point size is not always the best choice. If the label should respect the user’s text-size preferences, use text styles and dynamic type.

swift
label.font = UIFont.preferredFont(forTextStyle: .title2)
label.adjustsFontForContentSizeCategory = true

This allows the label to scale with accessibility settings instead of remaining locked to one size. It is often the better default for content labels, while fixed sizes remain more appropriate for tightly controlled branding or decorative UI.

Let the Label Resize Correctly

Changing the font can also affect layout. A larger font may need multiple lines or updated constraints.

swift
label.numberOfLines = 0
label.textAlignment = .center

If the label is clipped after increasing the font size, the problem is often not the font API itself. The real problem is a frame or constraint that no longer fits the text.

When using frames instead of Auto Layout, call sizeToFit() if appropriate:

swift
label.font = UIFont.systemFont(ofSize: 28)
label.sizeToFit()

That updates the label’s frame to match the new rendered text size.

Make Reusable Styling Helpers

If the same font rules appear in multiple screens, wrap them in a helper so the intent stays consistent.

swift
1func applyHeadlineStyle(to label: UILabel) {
2    label.font = UIFont.systemFont(ofSize: 24, weight: .bold)
3    label.textColor = .label
4    label.numberOfLines = 0
5}

This reduces one-off styling drift and makes future updates easier.

Common Pitfalls

  • Setting a larger font size without updating constraints or allowing line wrapping.
  • Using a custom font name that is not actually registered in the app bundle.
  • Choosing fixed sizes everywhere and ignoring dynamic type for content-heavy screens.
  • Calling UIFont.systemFont(ofSize:) when the design really needs a specific weight or text style.
  • Changing the font but forgetting that frame-based layouts may need sizeToFit().

Summary

  • Set a UILabel font size by assigning a new UIFont to the font property.
  • Use weighted system fonts when the label needs emphasis as well as size.
  • Handle custom fonts safely with a fallback.
  • Prefer dynamic type for content that should respect accessibility text settings.
  • If the text clips after resizing, fix the layout rather than the font call.

Course illustration
Course illustration