UILabel is not auto-shrinking text to fit label size
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UILabel can shrink its font to fit the available width, but only in a fairly specific layout scenario. If the label still truncates or overflows, the problem is usually configuration rather than a UIKit bug.
The main rule is simple: auto-shrinking is designed for single-line labels with a known width. Once you understand that constraint, the behavior becomes much easier to debug.
The Required Properties
Three properties matter most:
- '
adjustsFontSizeToFitWidth = true' - '
minimumScaleFactorset to a reasonable lower bound such as0.7' - '
numberOfLines = 1'
If numberOfLines is zero or greater than one, UILabel does not perform the same width-based shrinking behavior people usually expect.
Auto Layout Must Resolve the Width
The label also needs a real width at layout time. If the frame is still zero, ambiguous, or wider than you think because constraints are incomplete, there is nothing meaningful to shrink against.
This works because Auto Layout gives the label a definite width between the leading and trailing anchors.
Use the Right Tool for Multi-Line Text
If you want two or more lines that adapt to varying content, shrinking is usually the wrong solution. Multi-line text is better handled with line wrapping, dynamic type, or a different layout.
For example, a long body string in a settings screen should usually wrap naturally rather than shrink to an unreadably small font. Auto-shrinking is best for short labels such as titles, prices, or tab-like text where keeping one line matters.
Check for Attributed Text and Layout Timing
Attributed strings can override font settings in ways that make shrinking appear inconsistent. Also, if you are inspecting the label before layout has happened, you may think shrinking is broken when the final frame has not been calculated yet.
A simple debug step is to call view.layoutIfNeeded() before measuring or snapshotting the UI. That forces constraints to resolve first.
Common Pitfalls
- Expecting auto-shrink to work on multi-line labels.
UILabelshrinking is primarily a single-line feature. - Forgetting to set
minimumScaleFactor. Without it, the label may not shrink as much as you expect. - Leaving the width ambiguous in Auto Layout. The label cannot fit text to a size it does not know.
- Using attributed text that overrides the font unexpectedly. The runtime result may differ from the plain label settings.
- Solving the wrong UX problem. If the text is long by design, wrapping or redesigning the layout is usually better than aggressive shrinking.
Summary
- '
UILabelauto-shrinking works best for single-line labels with a known width.' - Set
adjustsFontSizeToFitWidth,minimumScaleFactor, andnumberOfLines = 1together. - Make sure Auto Layout resolves the label width before expecting correct behavior.
- Prefer wrapping or layout changes for genuinely multi-line content.
- Debug with final layout state, not with an unresolved or ambiguous frame.
Related reading
- UIlabel layer.cornerRadius not working in iOS 7.1
- UILabel sizeToFit doesn't work with autolayout ios6
- UILabel text margin
- UILabel with text of two different colors
- UILabel with text of two different colors
- UINavigationBar - Set title programmatically?
- UINavigationBar custom back button without title
- UINavigationBar Hide back Button Text
.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.