UILabel
line break
Swift
iOS development
Xcode

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:

  1. numberOfLines: Determines the maximum number of lines that the label can display.
  2. lineBreakMode: Defines how the text should wrap or truncate when it reaches the edge of the label's bounds.
  3. preferredMaxLayoutWidth: Helps in determining the wrapping width of the text when using Auto Layout.

Property Configuration

numberOfLines

  • Description: Controls the number of lines the UILabel will 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

swift
1let multiLineLabel = UILabel()
2
3// Set your label text
4multiLineLabel.text = "This is an example of a UILabel that spans\nmultiple lines. It demonstrates how line\nbreaks can be effectively managed in\nUIKit applications."
5
6// Configure line break properties
7multiLineLabel.numberOfLines = 0
8multiLineLabel.lineBreakMode = .byWordWrapping
9
10// Important for Auto Layout
11multiLineLabel.preferredMaxLayoutWidth = 200.0
12
13// Optionally set other appearance properties
14multiLineLabel.font = UIFont.systemFont(ofSize: 16)
15multiLineLabel.textColor = UIColor.black
16
17// Add the label to a view
18view.addSubview(multiLineLabel)
19multiLineLabel.translatesAutoresizingMaskIntoConstraints = false
20
21// Set layout constraints
22NSLayoutConstraint.activate([
23    multiLineLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
24    multiLineLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
25    multiLineLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 20)
26])

In the example above:

  • The numberOfLines is set to 0 to allow the label to grow based on content.
  • The lineBreakMode is set to .byWordWrapping for word boundary wrapping.
  • The preferredMaxLayoutWidth is set to 200.0 to define the desired width constraint, which assists the Auto Layout engine in calculating the height needed for text wrapping.

Summary Table

PropertyDescriptionDefault ValueCustom Value for Line Break
numberOfLinesMaximum number of lines the label can use.10 (unlimited)
lineBreakModeDefines text wrapping or truncation behavior..byTruncatingTail.byWordWrapping
preferredMaxLayoutWidthMaximum width for the label, crucial for Auto Layout.None200.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.


Course illustration
Course illustration

All Rights Reserved.