iOS
UILabel
Auto Layout
Multi-line Text
iOS Development

iOS Multi-line UILabel in Auto Layout

Master System Design with Codemia

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

Introduction

Making a UILabel display multiple lines under Auto Layout is mostly about giving it the right constraints and letting it grow vertically. The label itself is simple, but ambiguous width, missing constraints, or incorrect priorities can keep it stuck on one line or clip text unexpectedly. A reliable setup combines numberOfLines = 0 with clear horizontal constraints and, when needed, explicit content-priority tuning.

The Minimum Required Settings

At the label level, the first requirement is:

swift
label.numberOfLines = 0

That allows as many lines as needed. But it is not enough by itself. Auto Layout also needs to know the label width so it can compute where lines should wrap.

Give the Label a Defined Width Through Constraints

The most common pattern is pinning leading and trailing anchors to a container.

swift
1import UIKit
2
3let label = UILabel()
4label.numberOfLines = 0
5label.translatesAutoresizingMaskIntoConstraints = false
6label.text = "This is a long message that should wrap across multiple lines in Auto Layout."
7
8view.addSubview(label)
9
10NSLayoutConstraint.activate([
11    label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
12    label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
13    label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20)
14])

Now Auto Layout knows the label's horizontal space, so it can compute height correctly.

Why Width Is the Real Requirement

A multi-line label needs to know when a line ends. Without width information, the engine cannot decide where wrapping should happen. That is why labels often fail in these cases:

  • only top and center constraints exist
  • width is left ambiguous
  • container view itself has ambiguous width

Developers often blame numberOfLines, but the missing width constraint is the real issue.

Content Hugging and Compression Resistance

In stack views or crowded layouts, priorities can decide whether the label expands or gets compressed.

swift
label.setContentCompressionResistancePriority(.required, for: .vertical)
label.setContentHuggingPriority(.defaultLow, for: .horizontal)

This is especially useful when labels sit beside icons, buttons, or fixed-width controls.

preferredMaxLayoutWidth

In modern Auto Layout, preferredMaxLayoutWidth is often inferred once the label has constraints, but there are still cases where setting it manually helps, particularly in custom layout timing or older code patterns.

swift
1override func viewDidLayoutSubviews() {
2    super.viewDidLayoutSubviews()
3    label.preferredMaxLayoutWidth = label.bounds.width
4}

You usually do not need this in simple layouts, but it remains a useful fix for stubborn wrapping behavior.

Multi-Line Labels in Table and Collection Cells

In reusable cells, the same rules apply:

  1. numberOfLines = 0
  2. full horizontal constraints to contentView
  3. enough vertical constraints so cell height can be derived

If cell height still looks wrong, the issue is usually the cell layout, not the label itself.

Text Alignment and Line Break Mode

You can still control how wrapped lines appear.

swift
label.textAlignment = .left
label.lineBreakMode = .byWordWrapping

Word wrapping is the normal choice for readable body text. Character wrapping is rarely desirable outside special UI cases.

Dynamic Type and Localization

Multi-line labels should be tested with:

  • larger accessibility font sizes
  • longer localized strings
  • right-to-left layouts

What fits in English on a simulator may take three lines in another language. Auto Layout should be allowed to expand naturally instead of relying on hardcoded heights.

Common Pitfalls

  • Setting numberOfLines = 0 but leaving the label width ambiguous.
  • Pinning the label with fixed height so it cannot grow vertically.
  • Ignoring content compression priorities in crowded horizontal layouts.
  • Blaming the label when the container view or cell constraints are incomplete.
  • Hardcoding text heights that fail under dynamic type or localization.

Summary

  • Multi-line UILabel support starts with numberOfLines = 0.
  • Auto Layout must know the label width before wrapping can work.
  • Full horizontal constraints are usually the most important piece.
  • Tune hugging and compression priorities when labels share space with other views.
  • Test with dynamic type and long localized strings instead of only short sample text.

Course illustration
Course illustration