Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text.lineLimit() behavior is inconsistent in SwiftUI

Tags:

ios

swift

swiftui

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

like image 954
Dominic Holmes Avatar asked Jun 14 '19 17:06

Dominic Holmes


2 Answers

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.

like image 148
Wil Shipley Avatar answered Nov 11 '22 16:11

Wil Shipley


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) {
       ...
    }
}
like image 1
jeh Avatar answered Nov 11 '22 16:11

jeh