Swift - UIButton with two lines of text
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIButton can display two lines of text, but only if both the title label and the layout constraints allow it. Most failures come from one of two things: the label is still configured for a single line, or the button frame is too tight for wrapped text. The fix is usually simple once you separate title configuration from layout behavior.
Core Sections
Basic Multiline Button Setup
The simplest approach is to put a newline in the title and allow multiple lines on the label:
The newline guarantees two lines. numberOfLines = 0 allows wrapping instead of forcing a single line.
Auto Layout Must Leave Room for Wrapping
Many two-line button problems are not about UIButton at all. The title is configured correctly, but the button height or width prevents the label from expanding.
If you want automatic wrapping without a manual newline, a width constraint is especially important. Without width pressure, the label often stays on one long line.
Wrap Long Titles Naturally
You do not have to force a newline. If the design should wrap based on available width, use a longer title and let the label break words:
This works well for responsive layouts where the exact line break depends on device size.
Use Attributed Titles for Different Styles Per Line
If the first and second lines need different fonts or colors, use an attributed string:
This gives you a headline-plus-subtitle look without building a custom control.
UIButton.Configuration on Modern iOS
On newer iOS versions, UIButton.Configuration is cleaner for basic styling:
Even with configurations, multiline behavior still depends on the underlying label and constraints.
When a Custom Control Is Better
If the button needs:
- an icon
- title and subtitle
- custom spacing
- fully controlled accessibility labels
then forcing everything through setTitle becomes limiting. In that case, build a UIControl or place a UIStackView inside a button-like container instead of stretching UIButton beyond its comfortable use case.
Dynamic Type and Accessibility
Two-line buttons can become three or four lines at larger text sizes. Test with accessibility font sizes and avoid hardcoded heights that only work at the default size.
A safer setup is:
Then use flexible height constraints rather than fixed small frames.
State Handling Still Matters
If you use different button states, configure them intentionally:
Otherwise the normal-state multiline title may look correct while disabled or highlighted states fall back to unexpected defaults.
Common Pitfalls
- Setting
lineBreakModebut forgettingnumberOfLines. - Giving the button a height that clips wrapped text.
- Expecting wrapping without a constrained width or explicit newline.
- Styling only the normal state and ignoring disabled or highlighted states.
- Hardcoding layout values that fail under Dynamic Type or localization.
Summary
- Multiline
UIButtontitles require both label configuration and sufficient layout space. - Use a newline for fixed two-line titles or width constraints for natural wrapping.
- Attributed titles are the right tool when each line needs different styling.
- Modern
UIButton.Configurationhelps with styling, but not with layout by itself. - If the design becomes too complex, use a custom control instead of forcing
UIButtonto do everything.
Related reading
- Swift - which types to use? NSString or String
- Swift - which types to use? NSString or String
- Swift 2.0 - Binary Operator cannot be applied to two UIUserNotificationType operands
- Swift 2.0 - Binary Operator cannot be applied to two UIUserNotificationType operands
- Swift 2 Call can throw, but it is not marked with ''try'' and the error is not handled
- Swift 2 Call can throw, but it is not marked with ''try'' and the error is not handled
- Swift 3.0 FileManager.fileExistsatPath always return false
- Swift 3.0 Result of call is unused
.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.