How to add line break for UILabel?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working with UILabel in iOS development, a common requirement is to handle text that spans multiple lines. By default, a UILabel is configured to be a single line, which means that it will truncate any overflowing text if it cannot fit within the specified bounds.
This article provides a comprehensive guide on how to add line breaks to a UILabel, discussing the necessary properties to configure and providing examples for different scenarios.
Understanding UILabel Line Breaks
UILabel can display multiple lines of text, but there are certain properties that must be configured to achieve this behavior. The three key properties related to line breaks in a UILabel are:
numberOfLines: Determines the maximum number of lines that the label can display.lineBreakMode: Defines how the text should wrap or truncate when it reaches the edge of the label's bounds.preferredMaxLayoutWidth: Helps in determining the wrapping width of the text when using Auto Layout.
Property Configuration
numberOfLines
- Description: Controls the number of lines the
UILabelwill use to lay out the text. - Default Value: 1, which means the label will be a single line by default.
- Set to 0: Allows the label to grow vertically to accommodate the text without constraints on the number of lines.
lineBreakMode
- Description: Indicates how to wrap or truncate text.
- Options:
.byWordWrapping: Wraps text at word boundaries..byCharWrapping: Wraps text at character boundaries..byTruncatingTail: Truncates text at the end with an ellipsis (...)..byTruncatingMiddle: Truncates text in the middle..byTruncatingHead: Truncates text at the start.
preferredMaxLayoutWidth
- Description: Specifies the maximum width for a label to support multiple lines of text.
- Usage: Particularly important when using Auto Layout, as it informs the layout system of the maximum line width for multiple-line labels.
Implementing Line Breaks
Let's explore an example to demonstrate how to add line breaks in a UILabel.
Example
In the example above:
- The
numberOfLinesis set to0to allow the label to grow based on content. - The
lineBreakModeis set to.byWordWrappingfor word boundary wrapping. - The
preferredMaxLayoutWidthis set to200.0to define the desired width constraint, which assists the Auto Layout engine in calculating the height needed for text wrapping.
Summary Table
| Property | Description | Default Value | Custom Value for Line Break |
numberOfLines | Maximum number of lines the label can use. | 1 | 0 (unlimited) |
lineBreakMode | Defines text wrapping or truncation behavior. | .byTruncatingTail | .byWordWrapping |
preferredMaxLayoutWidth | Maximum width for the label, crucial for Auto Layout. | None | 200.0 (example specific) |
Additional Considerations
Performance Considerations
While setting numberOfLines to 0 allows unlimited text wrapping, take note of performance impacts when dealing with a large amount of text. Always constrain the label to reasonable bounds and consider lazy loading techniques when using a collection of multi-line labels, like in a UITableView.
Localization and Dynamic Type
If your app supports multiple languages or dynamic text sizing, keep in mind that translated text might be longer or shorter. Test your layouts extensively to ensure text remains legible and aesthetically pleasing across different sizes and languages.
Conclusion
Adding line breaks to a UILabel involves a simple yet thoughtful setup of certain properties that control text layout. By leveraging these properties carefully (numberOfLines, lineBreakMode, and preferredMaxLayoutWidth), developers can create flexible and visually appealing multi-line labels suited for various app requirements.

