UILabel
dynamic height
iOS development
Swift
text rendering

How to calculate UILabel height dynamically?

Master System Design with Codemia

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

Introduction

The right way to calculate a UILabel's height depends on whether you are using Auto Layout or manual frames. In modern UIKit, Auto Layout is usually the preferred solution: constrain the label's width, set numberOfLines = 0, and let the layout system compute the height. Manual height calculation is still useful for precomputing cell sizes or custom drawing.

Auto Layout First

If your label is fully constrained horizontally, UIKit can derive the height from the text and font.

swift
1let label = UILabel()
2label.numberOfLines = 0
3label.font = .systemFont(ofSize: 17)
4label.translatesAutoresizingMaskIntoConstraints = false
5label.text = "A long block of text that should wrap across multiple lines."

The key requirement is a known width. Without a width constraint, UIKit cannot know where line wrapping should happen.

In a real layout, pin the leading and trailing anchors or give the label an explicit width.

Manual Height Calculation With boundingRect

When you need the height before layout happens, measure the text directly.

swift
1import UIKit
2
3func heightForText(_ text: String, width: CGFloat, font: UIFont) -> CGFloat {
4    let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
5    let boundingBox = text.boundingRect(
6        with: constraintRect,
7        options: [.usesLineFragmentOrigin, .usesFontLeading],
8        attributes: [.font: font],
9        context: nil
10    )
11    return ceil(boundingBox.height)
12}
13
14let height = heightForText(
15    "A long block of text that should wrap across multiple lines.",
16    width: 220,
17    font: .systemFont(ofSize: 17)
18)
19print(height)

The ceil is important so fractional layout values do not clip the last line.

Measuring a Real UILabel

If you already have the configured label instance, you can ask it for a fitting size.

swift
label.frame.size.width = 220
let size = label.sizeThatFits(CGSize(width: 220, height: .greatestFiniteMagnitude))
print(size.height)

This is convenient when the label has attributed text, custom line breaks, or other properties already applied.

Dynamic Table and Collection View Cells

In self-sizing cells, the cleanest approach is usually not to compute the label height manually at all. Instead:

  • set numberOfLines = 0
  • constrain the label correctly
  • enable automatic dimension sizing

That lets the table or collection view compute the final height from the content constraints.

Manual height calculation is still useful if you are implementing custom layout caching for performance.

Attributed Text Needs Matching Attributes

If the label uses attributedText, measure with the same attributes or use the label itself via sizeThatFits. Otherwise the measured height can be wrong because font, paragraph spacing, or line-height behavior differs from plain text.

Width Must Match Reality

The most common source of wrong heights is using the wrong width. Remember that the usable width is not always the full screen width. It may be reduced by:

  • layout margins
  • accessory views
  • stack view spacing
  • content insets
  • cell padding

If the width is wrong, the line wrapping changes and the calculated height becomes wrong too.

Common Pitfalls

A common mistake is leaving numberOfLines at 1. In that case the label cannot grow vertically, no matter how carefully you calculate.

Another mistake is measuring with one font and displaying with another. Height calculation must use the exact text attributes that the label will actually render.

Developers also often forget that Auto Layout already solves this problem well when the label has a known width and proper constraints.

Summary

  • Prefer Auto Layout for dynamic label height in modern UIKit.
  • Set numberOfLines = 0 and make the label's width unambiguous.
  • Use boundingRect or sizeThatFits when you need manual measurement.
  • Measure with the real font or attributed text settings.
  • Most wrong results come from using the wrong width, not the wrong formula.

Course illustration
Course illustration

All Rights Reserved.