12

I have the following and would like the longer text to wrap

Text("Long label that needs to be able to wrap but isn't doing it yet.")
    .font(.largeTitle)
    .multilineTextAlignment(.center)
    .lineLimit(0)
mwright
  • 4,099
  • 7
  • 30
  • 40

3 Answers3

11

Turns out you can pass nil to the .lineLimit and it will make the Text() wrap as desired.

Text("Long label that needs to be able to wrap but isn't doing it yet.")
    .font(.largeTitle)
    .multilineTextAlignment(.center)
    .lineLimit(nil)
mwright
  • 4,099
  • 7
  • 30
  • 40
  • 3
    I am using a `Text` instance inside of a `NavigationButton` instance. I am not seeing `Text` instances wrap as of β2 when used inside of such containers. Have you also seen this behavior? – Nick Kohrn Jun 27 '19 at 14:00
  • I haven't used that setup yet @NickKohrn – mwright Jul 02 '19 at 15:58
6

Both .fixedSize and .lineLimit(nil) should work.

 Text("Label text")
        .multilineTextAlignment(.leading)
        .fixedSize(horizontal: false, vertical: true)

OR

 Text("Label text")
        .multilineTextAlignment(.leading)
        .lineLimit(nil)

.fixedSize is the preferred/suggested option from Apple to word wrap the text into next line

Naishta
  • 11,885
  • 4
  • 72
  • 54
2

I just tested this on the lastest XCode 11 beta, beta 7. I needed to specify a non-nil line limit and also use the padding modifier in order to achieve multiline text

Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.")
   .font(.subheadline)
   .multilineTextAlignment(.center)
   .lineLimit(3)
   .padding()
Robert
  • 981
  • 1
  • 15
  • 24