UILabel
font size
font name
iOS development
Swift programming

How can I get the font size and font name of a UILabel?

Master System Design with Codemia

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

Introduction

A UILabel stores its text styling in a UIFont object, so reading the current font name and size is straightforward once you know where to look. This is useful when debugging layout issues, copying a label's style, or adapting UI behavior at runtime.

Accessing the Font on a UILabel

Every UILabel has a font property. That property is a UIFont, and UIFont exposes the details you usually want: the display name and the point size.

Here is the simplest example in Swift:

swift
1import UIKit
2
3let label = UILabel()
4label.font = UIFont(name: "AvenirNext-DemiBold", size: 18)
5label.text = "Profile"
6
7print(label.font.fontName)
8print(label.font.pointSize)

This prints the current font name and size for the label. If you are using a system font, the name will reflect the actual system font variant in use.

Reading Font Details in a Real View Controller

In a real application, you usually inspect a label after outlets are connected or after the view is loaded.

swift
1import UIKit
2
3final class ProfileViewController: UIViewController {
4    @IBOutlet private weak var titleLabel: UILabel!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        let font = titleLabel.font
10        print("Font name: \(font.fontName)")
11        print("Font size: \(font.pointSize)")
12    }
13}

This is the correct place to inspect most interface builder labels, because the nib or storyboard has already applied its configured font.

Getting a Human-Friendly Description

Sometimes you want to log the full style information, not just name and size. Creating a helper makes that easier and keeps debugging code tidy.

swift
1import UIKit
2
3func describeFont(of label: UILabel) -> String {
4    let font = label.font
5    return "name=\(font.fontName), size=\(font.pointSize)"
6}
7
8let label = UILabel()
9label.font = .preferredFont(forTextStyle: .headline)
10print(describeFont(of: label))

This is useful when you are comparing several labels or verifying that design tokens are applied correctly.

Working with Dynamic Type

A label may use a preferred text style rather than a fixed custom font. In that case, the font property still gives you the active UIFont, but the value can change when content size settings change.

swift
1import UIKit
2
3let label = UILabel()
4label.font = .preferredFont(forTextStyle: .body)
5label.adjustsFontForContentSizeCategory = true
6
7print(label.font.fontName)
8print(label.font.pointSize)

If you inspect the font once at launch and assume it never changes, your layout logic may drift out of sync with accessibility settings. Re-read the label's font when responding to content size updates.

Copying Font Information to Another Label

Reading font details is often the first step toward reusing them.

swift
1import UIKit
2
3let sourceLabel = UILabel()
4sourceLabel.font = UIFont(name: "Georgia-Bold", size: 20)
5
6let destinationLabel = UILabel()
7destinationLabel.font = sourceLabel.font
8
9print(destinationLabel.font.fontName)
10print(destinationLabel.font.pointSize)

Because UIFont is the full style object, you usually do not need to copy fontName and pointSize separately unless you are reconstructing the font intentionally.

When the Font Name Is Not What You Expect

The font name returned by UIKit is the PostScript-style name, not always the marketing name you see in design tools. That is normal. For example, the system font may report a variant that looks more technical than the label shown in the design spec.

For custom fonts, make sure the exact internal font name is used. The filename inside your project bundle may differ from font.fontName.

Common Pitfalls

One common mistake is trying to read the font before the label is fully initialized. If an outlet has not been connected yet, you may be inspecting a default label instead of the one configured in Interface Builder.

Another issue is assuming the font size equals the label's visible text height. A pointSize is the font setting, not the final rendered height after line spacing, constraints, and text wrapping are applied.

Developers also sometimes compare only the font name and ignore traits such as weight or dynamic type scaling. If style consistency matters, compare the full UIFont behavior rather than just one string.

Finally, if a custom font returns nil during creation, the label may silently fall back to the system font. Always verify both the configured font and the reported fontName when debugging.

Summary

  • Read label.font.fontName to get the font's name.
  • Read label.font.pointSize to get the size in points.
  • Inspect labels after outlets and storyboard configuration are loaded.
  • Dynamic type can change the active font at runtime.
  • 'UIFont can be copied directly when you want another label to match.'
  • The reported font name may differ from the filename or design-tool label.

Course illustration
Course illustration

All Rights Reserved.