Text.lineLimit() behavior is inconsistent in SwiftUI

Dom*_*mes 8 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()
        }
Run Code Online (Sandbox Code Playgroud)

Here's an image of what's happening:

在此处输入图片说明

Wil*_*ley 29

您也可能会在 Xcode 11 GM 的这个答案中得到帮助:

/sf/answers/3962321961/

总结是,在其他构建器中,您需要将其添加.fixedSize(horizontal: false, vertical: true)到 Text() 以使其包装。

  • 哇。非常感谢 Apple 提供了一种非常直观的方式来实现这一目标。</讽刺> (5认同)