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.
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.
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.
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 = 0and make the label's width unambiguous. - Use
boundingRectorsizeThatFitswhen 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.

