UILabel
auto shrink
text fitting
iOS development
Swift programming

UILabel is not auto-shrinking text to fit label size

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

UILabel can shrink its font to fit the available width, but only in a fairly specific layout scenario. If the label still truncates or overflows, the problem is usually configuration rather than a UIKit bug.

The main rule is simple: auto-shrinking is designed for single-line labels with a known width. Once you understand that constraint, the behavior becomes much easier to debug.

The Required Properties

Three properties matter most:

  • 'adjustsFontSizeToFitWidth = true'
  • 'minimumScaleFactor set to a reasonable lower bound such as 0.7'
  • 'numberOfLines = 1'
swift
1import UIKit
2
3let label = UILabel()
4label.text = "A very long title that may not fit"
5label.font = .systemFont(ofSize: 24, weight: .semibold)
6label.adjustsFontSizeToFitWidth = true
7label.minimumScaleFactor = 0.7
8label.numberOfLines = 1
9label.lineBreakMode = .byClipping

If numberOfLines is zero or greater than one, UILabel does not perform the same width-based shrinking behavior people usually expect.

Auto Layout Must Resolve the Width

The label also needs a real width at layout time. If the frame is still zero, ambiguous, or wider than you think because constraints are incomplete, there is nothing meaningful to shrink against.

swift
1import UIKit
2
3final class TitleViewController: UIViewController {
4    private let label = UILabel()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        view.backgroundColor = .white
9
10        label.translatesAutoresizingMaskIntoConstraints = false
11        label.text = "A very long title that may not fit"
12        label.font = .systemFont(ofSize: 24, weight: .bold)
13        label.adjustsFontSizeToFitWidth = true
14        label.minimumScaleFactor = 0.6
15        label.numberOfLines = 1
16
17        view.addSubview(label)
18        NSLayoutConstraint.activate([
19            label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
20            label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
21            label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20)
22        ])
23    }
24}

This works because Auto Layout gives the label a definite width between the leading and trailing anchors.

Use the Right Tool for Multi-Line Text

If you want two or more lines that adapt to varying content, shrinking is usually the wrong solution. Multi-line text is better handled with line wrapping, dynamic type, or a different layout.

For example, a long body string in a settings screen should usually wrap naturally rather than shrink to an unreadably small font. Auto-shrinking is best for short labels such as titles, prices, or tab-like text where keeping one line matters.

Check for Attributed Text and Layout Timing

Attributed strings can override font settings in ways that make shrinking appear inconsistent. Also, if you are inspecting the label before layout has happened, you may think shrinking is broken when the final frame has not been calculated yet.

A simple debug step is to call view.layoutIfNeeded() before measuring or snapshotting the UI. That forces constraints to resolve first.

Common Pitfalls

  • Expecting auto-shrink to work on multi-line labels. UILabel shrinking is primarily a single-line feature.
  • Forgetting to set minimumScaleFactor. Without it, the label may not shrink as much as you expect.
  • Leaving the width ambiguous in Auto Layout. The label cannot fit text to a size it does not know.
  • Using attributed text that overrides the font unexpectedly. The runtime result may differ from the plain label settings.
  • Solving the wrong UX problem. If the text is long by design, wrapping or redesigning the layout is usually better than aggressive shrinking.

Summary

  • 'UILabel auto-shrinking works best for single-line labels with a known width.'
  • Set adjustsFontSizeToFitWidth, minimumScaleFactor, and numberOfLines = 1 together.
  • Make sure Auto Layout resolves the label width before expecting correct behavior.
  • Prefer wrapping or layout changes for genuinely multi-line content.
  • Debug with final layout state, not with an unresolved or ambiguous frame.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.