Adjust UILabel height to text
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Making a UILabel automatically resize its height to fit its text content is one of the most common iOS layout tasks. The key is setting numberOfLines = 0 (unlimited lines) and configuring Auto Layout constraints correctly. With proper setup, the label's intrinsic content size drives the layout, and the label grows or shrinks as text changes.
Method 1: Auto Layout (Recommended)
The simplest approach — let Auto Layout handle everything:
With numberOfLines = 0 and leading/trailing constraints, the label calculates its own height based on the text, font, and available width.
Method 2: Interface Builder
In Storyboard/XIB:
- Select the UILabel
- Set Lines to
0in the Attributes Inspector - Add leading, trailing, and top (or vertical position) constraints
- Do not add a height constraint
- Set Content Hugging Priority (Vertical) to
251(default) and Content Compression Resistance (Vertical) to750
The label will resize to fit its content at runtime.
Method 3: sizeToFit() (Manual Frames)
If using manual frame layout instead of Auto Layout:
Method 4: sizeThatFits / boundingRect
Calculate the required height before setting the frame:
Method 5: In UITableViewCell
For dynamic-height table view cells:
The bottom constraint connecting the label to contentView.bottomAnchor is critical — it tells the cell to size itself based on the label's height.
Method 6: Attributed Text
For labels with mixed formatting:
preferredMaxLayoutWidth
When a label's width is ambiguous (e.g., in a cell before layout), set preferredMaxLayoutWidth to tell the label what width to use for line wrapping calculations:
This is typically needed only when Auto Layout alone cannot determine the width.
Common Pitfalls
- numberOfLines not set to 0: The default is
1, which truncates text. Always setnumberOfLines = 0for multiline labels. - Missing width constraint: Without a leading+trailing or width constraint, the label has no maximum width and will try to fit all text on one line, extending off-screen.
- Height constraint conflicts: If you accidentally add a height constraint, it overrides the intrinsic content size. Remove it or set its priority below 750.
- Content compression resistance: If other views are competing for space, ensure the label's vertical compression resistance priority (default 750) is high enough to prevent truncation.
- sizeToFit() before setting text:
sizeToFit()uses the current text. Call it after settingtextandfont, not before.
Summary
- Set
numberOfLines = 0and provide leading/trailing constraints — no height constraint needed - Auto Layout uses the label's intrinsic content size to determine height automatically
- For table view cells, pin the label to all four edges of
contentViewand useautomaticDimension - Use
sizeThatFits()orboundingRectwhen calculating height manually preferredMaxLayoutWidthresolves ambiguous-width situations
Related reading
- Adjusting the Xcode iPhone simulator scale and size
- AFNetworking 2.0 add headers to GET request
- AFNetworking Post Request
- After switching to Xcode 7, app size grew from 9 MB to 60 MB, is there a fix?
- After upgrading to Xcode 11.2 from Xcode 11.1, app crashes due to _UITextLayoutView
- After upgrading to Xcode 11.2 from Xcode 11.1, app crashes due to _UITextLayoutView
- Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo. when using GCC
- AlamoFire asynchronous completionHandler for JSON request
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.