24

I have some description text in a VStack, and I'd like to limit it to 3 lines. My understanding is that I modify Text() with a .lineLimit(3) modifier. However, when I do this, some of the descriptions get capped at 3 lines, while others get capped at 1. There doesn't seem to be any consistency as to where this happens.

I thought this could be the order in which I'm calling the modifier attributes, but switching the order of .font(.body) and .lineLimit doesn't change anything. I also tried removing the .padding(), and that doesn't work either.

List(clubData) { club in
            VStack(alignment: .leading) {

                Text(club.name)
                    .font(.title)
                    .lineLimit(nil)

                Text(club.subtitle)
                    .lineLimit(4)
                    .font(.body)
            }
            .padding()
        }

Here's an image of what's happening:

enter image description here

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Dominic Holmes
  • 581
  • 5
  • 13

5 Answers5

36

You might also be helped with this answer for the Xcode 11 GM:

https://stackoverflow.com/a/56604599/30602

The summary is that inside other Builders you need to add .fixedSize(horizontal: false, vertical: true) to your Text() to get it to wrap.

Wil Shipley
  • 9,343
  • 35
  • 59
1

I've experienced this.

For now if you wrap your views in a GeometryReader then it should respect the line limits

e.g.

GeometryReader { _ in
    VStack(alignment: .leading) {
       ...
    }
}
jeh
  • 2,373
  • 4
  • 23
  • 38
  • Thank you! Wrapping my Text view in Geometry reader solved my issue with text shrinkage on device rotation while using .font(.system(size: 120)), .minimumScaleFactor(0.1), and .lineLimit(1) – Ryan Nov 08 '19 at 06:53
1

The answer is to download Xcode 11.2 BETA. Xcode 11 GM in The App Store also have the problem with .linelimit(nil) being capped at one line.

Sondergaard
  • 1,121
  • 1
  • 7
  • 6
1

Dec 12th, 2019 with Xcode 11.2.1

DO NOT trust .lineLimit, use frame to set appropriate height (according to your font size) to wrap your multi-lined text. And, DO NOT use .lineSpacing even you think you make enough height, SwiftUI must have a really 'special' way to determine if the VStack height is enough and how many lines to display.

ZhouX
  • 1,866
  • 18
  • 22
0

April 23, 2020, Xcode 11.4

In my case, I get movie description text from TMDB API, which is in dynamic length. And it is embedded in VStack.

It is really annoyed. Even I set .linelimit(nil), the line for description text, it will still have a limit and end up ignoring some text as "long decription...".

Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32