How to set top-left alignment for UILabel for iOS application?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UILabel in UIKit centers text vertically by default — if the label's frame is taller than the text, the text sits in the middle. There is no built-in verticalAlignment property. To align text to the top-left, you have two main approaches: call sizeToFit() to shrink the label to fit its content, or subclass UILabel and override drawText(in:) to control where the text is drawn. For horizontal alignment, set textAlignment = .left. This article covers all approaches with practical examples.
Using sizeToFit() (Simplest Approach)
The easiest way to top-left align text is to let the label resize to fit its content:
After sizeToFit(), the label's frame shrinks to exactly fit the text, so there is no extra vertical space for centering to occur. The text naturally appears at the top-left.
Using Auto Layout (Recommended)
With Auto Layout, constrain the label to the top and leading edges without a fixed height. The label's intrinsic content size handles vertical fitting:
Without a height constraint, the label uses its intrinsic content size. The text starts at the top because there is no extra vertical space.
Subclassing UILabel for Vertical Alignment
When the label must have a fixed height larger than its text (e.g., in a table cell), subclass UILabel to control vertical alignment:
By passing a smaller rect to super.drawText(in:), the text is drawn at the top of the label instead of vertically centered.
Supporting All Vertical Alignments
Extend the subclass to support top, center, and bottom alignment:
Using UITextView as an Alternative
UITextView naturally top-aligns its text. If you need a non-editable text display with top alignment:
Setting isScrollEnabled = false makes the text view behave like a label that grows to fit its content. The text is top-aligned by default.
Using a Stack View
Another approach wraps the label in a UIStackView with top alignment:
Common Pitfalls
- Setting
textAlignmentand expecting vertical changes:textAlignmentonly controls horizontal alignment (.left,.center,.right,.natural). There is no built-in vertical alignment property onUILabel— you must use one of the workarounds described above. - Calling
sizeToFit()before setting text or constraints:sizeToFit()calculates size based on the current text and font. Calling it before settingtextornumberOfLinesproduces a zero-height label. Always configure the label fully before callingsizeToFit(). - Forgetting
numberOfLines = 0for multi-line text: The defaultnumberOfLinesis 1, which truncates text to a single line. Set it to 0 for unlimited lines, or the label will not expand vertically to fit all the text. - Using a fixed height constraint with Auto Layout: If you set a height constraint on a label, Auto Layout cannot shrink it to fit the text, and the default vertical centering takes effect. Remove the height constraint or use
>=constraints to let the label size to its content. - Not invalidating layout after changing
verticalAlignment: In the custom subclass, changingverticalAlignmentmust trigger a redraw. CallsetNeedsDisplay()in the property'sdidSetobserver, or the label will show stale alignment until the next layout pass.
Summary
- Set
textAlignment = .leftfor horizontal left alignment - For vertical top alignment, remove fixed height constraints and let the label size to its content (preferred approach)
- Use
sizeToFit()for frame-based layouts to shrink the label to fit text - Subclass
UILabeland overridedrawText(in:)when a fixed-height label must display top-aligned text - Consider
UITextViewwithisEditable = falseas an alternative — it top-aligns text by default - With Auto Layout, omit the height constraint so the label's intrinsic content size controls its height
Related reading
- How to set UICollectionViewCell Width and Height programmatically
- How to set UICollectionViewDelegateFlowLayout?
- How to set UITextField height?
- How to share an image on Instagram in iOS?
- How to show a dialog to confirm that the user wishes to exit an Android Activity?
- How to show an empty view with a RecyclerView?
- How to show Done button on iOS number pad keyboard?
- How to show soft-keyboard when edittext is focused
.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.