SwiftUI
Text Wrapping
iOS Development
Swift
User Interface Design

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 issue is usually layout constraints, not the Text view itself. SwiftUI decides wrapping based on available width from parent containers. Correct wrapping behavior comes from giving text flexible width and allowing multiple lines.

Basic Wrapping Setup

Start with lineLimit(nil) or a positive line limit and ensure text can grow vertically.

swift
1import SwiftUI
2
3struct WrappedTextView: View {
4    let message = "This is a long sentence that should wrap onto multiple lines in SwiftUI layouts."
5
6    var body: some View {
7        Text(message)
8            .lineLimit(nil)
9            .fixedSize(horizontal: false, vertical: true)
10            .padding()
11    }
12}

fixedSize with horizontal false and vertical true often resolves truncation in constrained stacks.

Parent Layout Matters

If parent containers force a tight horizontal size, text may truncate. Provide explicit flexible width.

swift
1VStack(alignment: .leading) {
2    Text("Long text that should wrap naturally when space is available.")
3        .lineLimit(nil)
4        .frame(maxWidth: .infinity, alignment: .leading)
5}
6.padding()

This tells SwiftUI to use available width and wrap instead of clipping.

Common Cases with Lists and HStacks

Inside HStack, neighboring views can squeeze text. Use layout priority.

swift
1HStack {
2    Image(systemName: "info.circle")
3    Text("Long descriptive text that needs room to wrap.")
4        .lineLimit(nil)
5        .layoutPriority(1)
6}

layoutPriority helps text keep width when competing with other views.

Wrapping in Buttons and Labels

Text inside buttons can be constrained by default styles. Apply wrapping modifiers directly to text label.

swift
1Button(action: {}) {
2    Text("Very long button label that should wrap into multiple lines")
3        .multilineTextAlignment(.center)
4        .lineLimit(2)
5        .fixedSize(horizontal: false, vertical: true)
6}

Keep style and frame decisions close to wrapped text.

Debugging Layout Quickly

When wrapping fails, add temporary borders and background colors to see actual frame size.

swift
Text("Debug me")
    .border(Color.red)
    .background(Color.yellow.opacity(0.2))

Visual frame debugging makes parent constraint problems obvious.

Design Guidance

For dynamic type and accessibility, prefer natural wrapping over truncation. Test with larger font sizes and narrow device widths. Wrapping issues often appear only on smaller screens or localized text.

Design for content expansion early to avoid late UI regressions.

Wrapping with Dynamic Type and Accessibility

Text wrapping that looks fine at default font may fail at larger accessibility sizes. Test with dynamic type enabled and ensure layouts still allow expansion.

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

This pattern handles larger font scaling more gracefully.

Avoid Truncation from Parent Frames

Wrapping can fail when parent view applies fixed height or clipping.

swift
1VStack {
2    Text(longMessage)
3        .lineLimit(nil)
4        .fixedSize(horizontal: false, vertical: true)
5}
6.frame(maxWidth: .infinity)
7// avoid strict fixed height unless intentional

Check parent modifiers first when wrapping appears broken.

Multiline Alignment for Better Readability

Wrapped text often needs explicit alignment settings to avoid awkward centered paragraph behavior.

swift
Text(longMessage)
    .multilineTextAlignment(.leading)
    .lineLimit(nil)

Alignment improves readability in cards, forms, and list rows.

Localization Stress Testing

Longer localized strings can expose wrapping bugs not visible in English. Run preview or simulator tests with languages that expand text length to verify layout resilience.

A localization pass during UI review can prevent late production issues.

Preview-Based Diagnostics

Use SwiftUI previews with narrow widths to reproduce wrapping failures quickly.

swift
1#Preview {
2    WrappedTextView()
3        .frame(width: 220)
4}

Preview-driven checks speed up iteration when debugging layout constraints.

Common Pitfalls

  • Setting line limit but forgetting to provide expandable width.
  • Using fixed frames that are too narrow for readable wrapping.
  • Ignoring parent stack constraints that compress text.
  • Testing only one device size and missing real-world truncation.

Summary

  • SwiftUI text wraps when parent layout provides flexible width.
  • Use line limits, fixed-size behavior, and frame alignment together.
  • Increase layout priority in stacks when text competes for space.
  • Debug with temporary borders to understand actual geometry.

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.