How does one display Text UI elements with multiple wrapped lines within a SwiftUI list.
The following code produces the following Image, and I can't seem to find a way to make this work as intended.
let test = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Donec enim diam vulputate ut pharetra. Sed turpis tincidunt id aliquet risus feugiat in. Interdum velit laoreet id donec ultrices tincidunt arcu non. Lorem END"
var body: some View {
List(){
Text(test).lineLimit(nil)
Divider()
Text(test).fixedSize(horizontal: false, vertical: true)
Divider()
Text(test)
}
}
.lineLimit(nil) fails to work
.fixedSize(horizontal: false, vertical: true) also fails to work
Re-tested with Xcode 13.4 / macOS 12.4 - Text just works, below workaround not needed anymore.
Ok, here is for macOS (added tag as well for better visibility), not by-default, but achievable for List as well, if needed List's performance, etc. and without any hardcoding.

Tested with Xcode 11.4 / macOS 10.15.3
var body: some View {
GeometryReader { gp in
List {
Text(self.test)
.padding(.trailing)
.frame(width: gp.size.width)
Divider()
}
}
}
you need to use ScrollView, not List
import SwiftUI
struct ContentView: View {
let test = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Donec enim diam vulputate ut pharetra. Sed turpis tincidunt id aliquet risus feugiat in. Interdum velit laoreet id donec ultrices tincidunt arcu non. Lorem END"
var body: some View {
ScrollView {
Text(test).lineLimit(1).padding()
Text(test).lineLimit(3).padding()
Text(test).padding()
}.frame(maxWidth: 400)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With