iOS multiline label in Interface builder
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To make a UILabel show multiple lines in Interface Builder, the essential setting is Lines = 0. That tells the label it may use as many lines as needed, but it still also needs enough width and correct Auto Layout constraints or the text may remain truncated.
The Core Interface Builder Settings
In storyboard or XIB:
- Select the label
- Set
Linesto0 - Set
Line BreaktoWord Wrapor another wrapping mode
Those settings allow multiline text, but they do not guarantee that the label grows correctly in the final layout. Auto Layout still decides the actual frame.
Constraints Matter as Much as the Label Property
A multiline label needs a known width so UIKit can decide where to wrap. If the label has ambiguous horizontal constraints, Auto Layout cannot compute the right height.
For example, this usually works well:
- pin leading and trailing edges
- avoid a fixed height
- let the label grow vertically
If you give the label a fixed height that is too small, Lines = 0 will not help because the constraint prevents the label from expanding.
Example Setup in Code
Even if the label is created in Interface Builder, it helps to know what the equivalent code looks like:
In Interface Builder, numberOfLines is the Lines field, and lineBreakMode corresponds to the line-break setting in the Attributes Inspector.
Auto Layout Example
Here is a programmatic version that behaves like a well-configured Interface Builder label:
There is no height constraint here. That is intentional. The label computes its own height from the available width and the text content.
Older UIKit Detail: preferredMaxLayoutWidth
In older Auto Layout setups, especially on older iOS versions, developers sometimes needed to set preferredMaxLayoutWidth so the label knew its wrapping width during layout. Modern UIKit usually handles this automatically when constraints are clear, but you may still see this property in older codebases.
You usually do not need this in modern simple layouts, but it explains some older answers that mention it.
Dynamic Type and Localization
A label that works in English with a small font may fail in larger accessibility sizes or longer translated strings. Multiline labels are often the correct answer for resilient UI because they let content grow naturally.
That also means your constraints should be tested with:
- larger content sizes
- longer localized strings
- narrow screen widths
If the label truncates under those conditions, the issue is usually layout constraints, not the Lines property.
Common Pitfalls
- Setting
Lines = 0but keeping a fixed height constraint prevents the label from expanding. - Leaving the label width ambiguous gives Auto Layout no reliable basis for wrapping.
- Forgetting to change
Line Breakfrom truncation modes can make the label cut off text even when multiple lines are allowed. - Assuming storyboard preview guarantees runtime behavior is risky; constraint conflicts may still appear on real devices or with Dynamic Type.
- Copying older advice about
preferredMaxLayoutWidthinto every project adds noise when modern constraints already solve the problem.
Summary
- For a multiline
UILabelin Interface Builder, setLines = 0and use a wrapping line-break mode. - Give the label clear horizontal constraints and avoid unnecessary fixed-height constraints.
- If text still does not wrap, inspect the Auto Layout constraints before blaming the label itself.
- Older projects may mention
preferredMaxLayoutWidth, but in many modern layouts clear constraints are enough.
Related reading
- iOS Nested View Controllers view inside UIViewController's view?
- iOS Prefix.pch best practices
- iOS Private API Documentation
- IOS project showing error An internal error occurred. Editing functionality may be limited on xcode 7.1
- iOS push notification how to detect if the user tapped on notification when the app is in background?
- iOS push notification how to detect if the user tapped on notification when the app is in background?
- iOS RestKit how to send a request with a callback object userData set?
- iOS Safari – How to disable overscroll but allow scrollable divs to scroll normally?
.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.