Adjust UILabel height to text
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Adjusting a UILabel's height to fit its text content requires two settings: numberOfLines = 0 (unlimited lines) and proper Auto Layout constraints that allow vertical expansion. With these in place, UILabel's intrinsic content size automatically calculates the required height based on the text, font, and available width. For programmatic height calculation without Auto Layout, use boundingRect(with:options:attributes:context:) or sizeThatFits(). This article covers all approaches in both UIKit and SwiftUI.
Auto Layout (Recommended)
With numberOfLines = 0 and leading/trailing constraints (which define the available width), UILabel calculates its own height from the text. Do not add a height constraint — that overrides the intrinsic content size.
Interface Builder Setup
The intrinsic content size provides the height, so you only need horizontal and one vertical positioning constraint.
Programmatic Height Calculation
boundingRect calculates the bounding rectangle for text without creating a UILabel. Use ceil() to round up to avoid clipping the last line.
In UITableViewCell
Pin the label's top and bottom to contentView with UITableView.automaticDimension for self-sizing cells.
Attributed Text Height
SwiftUI
SwiftUI Text views automatically size to fit their content. No equivalent of numberOfLines = 0 is needed — multiline is the default.
Common Pitfalls
numberOfLinesleft at default (1): The default is1, which clips text to a single line. SetnumberOfLines = 0for unlimited lines before expecting the label to grow.- Adding a fixed height constraint: A height constraint overrides the intrinsic content size. If you need a maximum height, use
label.heightAnchor.constraint(lessThanOrEqualToConstant: 200)instead ofequalToConstant. - Missing bottom constraint in self-sizing cells: For
UITableView.automaticDimensionto work, the label must be pinned to both the top and bottom ofcontentView. A missing bottom constraint makes the cell height ambiguous. preferredMaxLayoutWidthnot set: In some layout configurations (especially with horizontal stack views), UILabel needspreferredMaxLayoutWidthset to know the maximum width for wrapping. Set it to the label's actual width inlayoutSubviews().- Using
sizeToFit()with Auto Layout:sizeToFit()sets the frame directly, which conflicts with Auto Layout constraints. Use Auto Layout constraints andnumberOfLines = 0instead — the intrinsic content size handles everything.
Summary
- Set
numberOfLines = 0and provide leading/trailing constraints — UILabel handles height automatically - Do not add a fixed height constraint — it overrides the intrinsic content size
- For self-sizing table view cells, pin the label to both top and bottom of
contentViewand setrowHeight = UITableView.automaticDimension - Use
boundingRect(with:options:attributes:context:)for programmatic height calculation - Use
ceil()on calculated heights to avoid subpixel clipping - SwiftUI
Textviews size automatically with no configuration needed
Related reading
- Adjust UILabel height to text
- 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
.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 courseTrack 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.