Adjust UILabel height to text
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 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

