Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - Multi-Lined text macOS

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

like image 946
EM77 Avatar asked Jun 16 '26 19:06

EM77


2 Answers

Update: fixed!

Re-tested with Xcode 13.4 / macOS 12.4 - Text just works, below workaround not needed anymore.

Original

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.

demo

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()
        }
    }
}
like image 198
Asperi Avatar answered Jun 18 '26 09:06

Asperi


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()
    }
}

enter image description here

like image 22
user3441734 Avatar answered Jun 18 '26 07:06

user3441734



Donate For Us

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