SwiftUI
text wrapping
UI design
app development
programming tips

The text doesn't get wrapped in swift UI

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When text does not wrap in SwiftUI, the real problem is usually layout, not the Text view itself. Text can wrap, but only if its parent gives it a finite width and enough priority to use more than one line.

That is why adding random modifiers often does nothing. You have to look at the surrounding stack, frame constraints, and compression behavior to understand why the text stays on one line or gets truncated.

The Most Common Fix

For many cases, especially inside HStack, this combination is enough:

swift
Text(longMessage)
    .lineLimit(nil)
    .fixedSize(horizontal: false, vertical: true)

lineLimit(nil) allows multiple lines. fixedSize(horizontal: false, vertical: true) tells SwiftUI not to compress the text vertically into a single-line layout.

If the text still does not wrap, the next thing to inspect is the parent layout.

Why Parent Layout Matters

SwiftUI wrapping depends on available width. If the parent gives the text infinite width or keeps squeezing it horizontally, the text may truncate instead of wrapping.

A classic example is an HStack with an icon and long text:

swift
1HStack(alignment: .top, spacing: 8) {
2    Image(systemName: "info.circle")
3
4    Text(longMessage)
5        .lineLimit(nil)
6        .fixedSize(horizontal: false, vertical: true)
7}
8.padding()

This often works, but if another sibling view aggressively takes width, the text can still lose the layout negotiation.

Use layoutPriority When Siblings Compete

Inside stacks, SwiftUI decides which views give up space first. If your text is being compressed too aggressively, give it a higher layout priority:

swift
1HStack(alignment: .top) {
2    Image(systemName: "doc.text")
3
4    Text(longMessage)
5        .lineLimit(nil)
6        .fixedSize(horizontal: false, vertical: true)
7        .layoutPriority(1)
8}

This tells SwiftUI that the text should keep its room before lower-priority sibling views do.

Give the Text a Real Width to Wrap Within

Text cannot wrap meaningfully if there is no effective width constraint. Sometimes the fix is to give the view a frame that expands to the available width:

swift
1Text(longMessage)
2    .frame(maxWidth: .infinity, alignment: .leading)
3    .lineLimit(nil)
4    .fixedSize(horizontal: false, vertical: true)

This is especially useful inside containers where alignment and width are ambiguous.

If you want left-aligned multiline text, pair it with:

swift
.multilineTextAlignment(.leading)

That controls line alignment, not wrapping itself, but the two are often confused.

A Full Example

Here is a practical example that wraps reliably in a card-style layout:

swift
1struct MessageCard: View {
2    let longMessage: String
3
4    var body: some View {
5        HStack(alignment: .top, spacing: 12) {
6            Image(systemName: "bubble.left")
7                .foregroundColor(.blue)
8
9            Text(longMessage)
10                .lineLimit(nil)
11                .fixedSize(horizontal: false, vertical: true)
12                .multilineTextAlignment(.leading)
13                .layoutPriority(1)
14        }
15        .frame(maxWidth: .infinity, alignment: .leading)
16        .padding()
17    }
18}

This works because the text has multiple lines enabled, enough layout priority, and a parent that gives it meaningful width.

What Usually Does Not Fix It

Some modifiers are often added hopefully but do not address the real issue:

  • '.multilineTextAlignment(.leading) changes alignment, not wrapping'
  • '.frame(width:) can help or hurt depending on the chosen width'
  • '.lineLimit(nil) alone is often not enough inside constrained stacks'

If the layout still fails, step back and inspect the whole hierarchy rather than stacking more text modifiers blindly.

Common Pitfalls

The biggest pitfall is assuming the problem lives entirely on the Text view. In SwiftUI, parent layout rules decide whether wrapping is even possible.

Another common issue is using HStack with sibling views that take too much width and never giving the text any layout priority.

Developers also confuse truncation and non-wrapping. The text is often allowed to be multiline in theory, but the parent is forcing it into a layout where truncation wins.

Finally, be careful with .fixedSize(). The common fix for wrapping is .fixedSize(horizontal: false, vertical: true), not a version that forces horizontal expansion and breaks the layout in a different way.

Summary

  • SwiftUI text wrapping is usually a layout-constraint problem, not a missing text feature.
  • Start with .lineLimit(nil) and .fixedSize(horizontal: false, vertical: true).
  • If the text is inside a stack, use layoutPriority(1) when sibling views compete for width.
  • Give the text a meaningful width with a suitable frame or parent container.
  • Inspect the whole layout tree if wrapping still fails, because the parent view is often the real cause.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.