Swift
UILabel
font size
iOS development
Xcode

How do I change the font size of a UILabel in Swift?

Master System Design with Codemia

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

Introduction

Changing the font size of a UILabel is simple at the API level, but the right choice depends on whether you want a fixed design size, a custom typeface, or text that respects the user's accessibility settings. In production iOS code, you usually need to think about sizing, line wrapping, and Dynamic Type together rather than treating font size as one isolated property.

Set the Font Directly

The most direct way to change a label's font size is to assign a new UIFont to its font property.

swift
1import UIKit
2
3let titleLabel = UILabel()
4titleLabel.text = "Welcome"
5titleLabel.font = UIFont.systemFont(ofSize: 24, weight: .semibold)

This is the right choice when the design calls for a specific point size and you are creating the label in code. The moment you assign a new font, the label uses that size for all of its text unless you later replace it with an attributed string.

If you are using Interface Builder, the same change can be made in the Attributes Inspector, but the underlying idea is identical: UILabel renders whatever UIFont you set.

Use Custom Fonts Safely

If the design system uses a custom font family, load it by name and handle failure explicitly. A missing font registration should not crash the app silently.

swift
1import UIKit
2
3let bodyLabel = UILabel()
4bodyLabel.text = "Signed in successfully"
5
6if let font = UIFont(name: "AvenirNext-Regular", size: 18) {
7    bodyLabel.font = font
8} else {
9    bodyLabel.font = UIFont.systemFont(ofSize: 18)
10}

This pattern is useful because the fallback keeps the screen readable while you diagnose a misconfigured font file or Info.plist entry.

Support Dynamic Type

For user-facing text, especially body copy, fixed sizes are often the wrong default. Dynamic Type lets the system scale text according to the user's accessibility and reading preferences.

swift
1import UIKit
2
3let messageLabel = UILabel()
4messageLabel.text = "Your order has been shipped"
5messageLabel.font = UIFont.preferredFont(forTextStyle: .body)
6messageLabel.adjustsFontForContentSizeCategory = true
7messageLabel.numberOfLines = 0

This approach is usually better than hard-coding 17 or 20 for common text styles. It also reduces the maintenance burden because the system handles scaling rules consistently across the app.

If you need a custom font and Dynamic Type, scale the font with UIFontMetrics.

swift
1import UIKit
2
3let baseFont = UIFont(name: "AvenirNext-Regular", size: 18) ?? UIFont.systemFont(ofSize: 18)
4let scaledFont = UIFontMetrics(forTextStyle: .body).scaledFont(for: baseFont)
5
6let descriptionLabel = UILabel()
7descriptionLabel.font = scaledFont
8descriptionLabel.adjustsFontForContentSizeCategory = true

That keeps the brand font while still respecting accessibility settings.

Fit Text Within the Available Space

Changing font size is only part of the layout story. A large font can clip or truncate if the label is constrained too tightly. For labels that may grow, set the line count and layout constraints deliberately.

swift
1import UIKit
2
3let subtitleLabel = UILabel()
4subtitleLabel.text = "A longer line of text that may wrap onto multiple lines"
5subtitleLabel.font = UIFont.systemFont(ofSize: 20)
6subtitleLabel.numberOfLines = 0
7subtitleLabel.lineBreakMode = .byWordWrapping

adjustsFontSizeToFitWidth exists, but it is usually best reserved for short, single-line labels where shrinking is acceptable.

swift
1let compactLabel = UILabel()
2compactLabel.text = "INV-2026-0001"
3compactLabel.font = UIFont.systemFont(ofSize: 18)
4compactLabel.adjustsFontSizeToFitWidth = true
5compactLabel.minimumScaleFactor = 0.7
6compactLabel.numberOfLines = 1

For normal body text, wrapping is often better than aggressive shrinking.

Style Only Part of the Text

If you need multiple font sizes inside one label, use an attributed string instead of the plain font property.

swift
1import UIKit
2
3let text = NSMutableAttributedString(string: "Total: ")
4text.append(NSAttributedString(string: "$42.00", attributes: [
5    .font: UIFont.boldSystemFont(ofSize: 28)
6]))
7
8let totalLabel = UILabel()
9totalLabel.attributedText = text

That gives you fine-grained control without splitting the content into several labels.

Common Pitfalls

A common mistake is setting a large font size and then forgetting that the label still has one line and tight width constraints. The text looks fine in code review but truncates on smaller devices.

Another issue is using fixed point sizes for all text, including body content, and unintentionally ignoring accessibility. If the label contains important information, prefer a text style with Dynamic Type support.

Developers also sometimes use adjustsFontSizeToFitWidth as a general layout fix. It can hide constraint problems and make text too small to read.

Finally, if a custom font does not appear, verify that the font file is included in the target and registered correctly. The code may be fine while the project configuration is wrong.

Summary

  • Set label.font with a UIFont to change the size directly.
  • Use UIFont.preferredFont and adjustsFontForContentSizeCategory for accessible text.
  • Scale custom fonts with UIFontMetrics instead of hard-coding one fixed size.
  • Handle wrapping and constraints so larger fonts do not clip or truncate unexpectedly.
  • Use attributed strings when only part of the label needs a different size.

Course illustration
Course illustration

All Rights Reserved.