iOS
multiline label
Interface Builder
Xcode
UIKit

iOS multiline label in Interface builder

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

To make a UILabel show multiple lines in Interface Builder, the essential setting is Lines = 0. That tells the label it may use as many lines as needed, but it still also needs enough width and correct Auto Layout constraints or the text may remain truncated.

The Core Interface Builder Settings

In storyboard or XIB:

  1. Select the label
  2. Set Lines to 0
  3. Set Line Break to Word Wrap or another wrapping mode

Those settings allow multiline text, but they do not guarantee that the label grows correctly in the final layout. Auto Layout still decides the actual frame.

Constraints Matter as Much as the Label Property

A multiline label needs a known width so UIKit can decide where to wrap. If the label has ambiguous horizontal constraints, Auto Layout cannot compute the right height.

For example, this usually works well:

  • pin leading and trailing edges
  • avoid a fixed height
  • let the label grow vertically

If you give the label a fixed height that is too small, Lines = 0 will not help because the constraint prevents the label from expanding.

Example Setup in Code

Even if the label is created in Interface Builder, it helps to know what the equivalent code looks like:

swift
1import UIKit
2
3let label = UILabel()
4label.numberOfLines = 0
5label.lineBreakMode = .byWordWrapping
6label.text = "This is a long string that should wrap onto multiple lines."

In Interface Builder, numberOfLines is the Lines field, and lineBreakMode corresponds to the line-break setting in the Attributes Inspector.

Auto Layout Example

Here is a programmatic version that behaves like a well-configured Interface Builder label:

swift
1import UIKit
2
3class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let label = UILabel()
8        label.translatesAutoresizingMaskIntoConstraints = false
9        label.numberOfLines = 0
10        label.lineBreakMode = .byWordWrapping
11        label.text = "A long paragraph of text that should wrap naturally across multiple lines."
12
13        view.addSubview(label)
14
15        NSLayoutConstraint.activate([
16            label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
17            label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
18            label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20)
19        ])
20    }
21}

There is no height constraint here. That is intentional. The label computes its own height from the available width and the text content.

Older UIKit Detail: preferredMaxLayoutWidth

In older Auto Layout setups, especially on older iOS versions, developers sometimes needed to set preferredMaxLayoutWidth so the label knew its wrapping width during layout. Modern UIKit usually handles this automatically when constraints are clear, but you may still see this property in older codebases.

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

You usually do not need this in modern simple layouts, but it explains some older answers that mention it.

Dynamic Type and Localization

A label that works in English with a small font may fail in larger accessibility sizes or longer translated strings. Multiline labels are often the correct answer for resilient UI because they let content grow naturally.

That also means your constraints should be tested with:

  • larger content sizes
  • longer localized strings
  • narrow screen widths

If the label truncates under those conditions, the issue is usually layout constraints, not the Lines property.

Common Pitfalls

  • Setting Lines = 0 but keeping a fixed height constraint prevents the label from expanding.
  • Leaving the label width ambiguous gives Auto Layout no reliable basis for wrapping.
  • Forgetting to change Line Break from truncation modes can make the label cut off text even when multiple lines are allowed.
  • Assuming storyboard preview guarantees runtime behavior is risky; constraint conflicts may still appear on real devices or with Dynamic Type.
  • Copying older advice about preferredMaxLayoutWidth into every project adds noise when modern constraints already solve the problem.

Summary

  • For a multiline UILabel in Interface Builder, set Lines = 0 and use a wrapping line-break mode.
  • Give the label clear horizontal constraints and avoid unnecessary fixed-height constraints.
  • If text still does not wrap, inspect the Auto Layout constraints before blaming the label itself.
  • Older projects may mention preferredMaxLayoutWidth, but in many modern layouts clear constraints are enough.

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.