Fix: UILabel Loses Multiline Wrapping Inside UIStackView

Fix: UILabel Loses Multiline Wrapping Inside UIStackView

Written by

in

Quick answer: A UILabel in a horizontal UIStackView with .fill alignment ignores numberOfLines = 0 because the stack view stretches the label to fill the axis instead of using its intrinsic height. Fix it by either embedding the label in a container UIView with constraints, or changing the stack view’s alignment to .center (or .top/.bottom) so it respects the label’s intrinsic content size.

Why this happens

A horizontal UIStackView with its default alignment = .fill forces every arranged subview to stretch vertically to match the stack view’s height. When the stack view calculates its own height, it does not consult the intrinsic content size of its arranged views while .fill is active. Consequently, a UILabel with numberOfLines = 0 and word wrapping enabled receives a compressed vertical space, causing it to truncate or render on a single line.

Apple’s documentation states: “For all alignments except the fill alignment, the stack view uses each arranged view’s intrinsicContentSize property when calculating its size perpendicular to the stack’s axis.” The word except is critical: .fill opts out of intrinsic sizing on the cross axis, so the label never gets the height it needs for multiple lines.

How to fix it

1. Embed the label in a container UIView (works with any alignment)

This is the most robust solution because it decouples the label’s sizing from the stack view’s alignment logic.

  1. In Interface Builder, select the UILabel.
  2. Choose Editor ▸ Embed In ▸ View. A new UIView becomes the stack view’s arranged subview.
  3. Add constraints pinning the label to the container’s edges (leading, trailing, top, bottom). Use standard spacing or zero constants.
  4. Ensure the label still has numberOfLines = 0 and lineBreakMode = .byWordWrapping.

The container view now adopts the label’s intrinsic height, and the stack view stretches the container (not the label directly). The label receives the vertical space it requests.

// Programmatic equivalent
let container = UIView()
let label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(label)
NSLayoutConstraint.activate([
    label.leadingAnchor.constraint(equalTo: container.leadingAnchor),
    label.trailingAnchor.constraint(equalTo: container.trailingAnchor),
    label.topAnchor.constraint(equalTo: container.topAnchor),
    label.bottomAnchor.constraint(equalTo: container.bottomAnchor)
])
stackView.addArrangedSubview(container)

2. Change the stack view’s alignment to .center (or .top / .bottom)

If you prefer not to add an extra view, switch the horizontal stack view’s alignment off .fill.

  1. Select the UIStackView in Interface Builder.
  2. In the Attributes inspector, change Alignment from Fill to Center (or Top / Bottom).
  3. Verify the label has numberOfLines = 0.

With .center, the stack view asks each arranged view for its intrinsic height and centers it vertically. The label expands to as many lines as needed.

stackView.alignment = .center   // or .top, .bottom
label.numberOfLines = 0

Trade-off: The label will no longer stretch to the full height of the stack view. If your design requires the label’s background or border to fill the row, use the container-view approach instead.

How to prevent it

  • Default to numberOfLines = 0 on every label that might wrap.
  • When adding a label to a horizontal stack view, set the stack view’s alignment to .center (or .top/.bottom) unless you specifically need .fill for other subviews.
  • If .fill is required for other views, embed the label in a container view from the start.
  • Use UIStackView‘s setCustomSpacing(_:after:) for fine-grained spacing instead of relying on label height to create visual separation.

FAQ

Does this affect vertical stack views?

No. A vertical UIStackView with .fill alignment stretches subviews horizontally, which is usually what you want. The label’s intrinsic height is respected along the stack’s axis, so multiline wrapping works without extra steps.

Can I keep .fill alignment and still get multiline labels?

Only by embedding the label in a container view (Fix 1). The container absorbs the .fill stretch while constraining the label to its intrinsic height.

What about UITextView or custom views?

The same principle applies: any view that relies on intrinsicContentSize perpendicular to the stack’s axis needs either a non-.fill alignment or a container view when inside a horizontal stack view with .fill alignment.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.