Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI multiline text always truncating at 1 line

Tags:

swiftui

I am trying to create a list of options for a user to choose from. The debug preview shows the general look and feel. My problem is that passing nil to .lineLimit in MultipleChoiceOption doesn't allow the text to grow beyond 1 line. How can I correct this?

struct Card<Content: View> : View {
    private let content: () -> Content
    init(content: @escaping () -> Content) {
        self.content = content
    }

    private let shadowColor = Color(red: 69 / 255, green: 81 / 255, blue: 84 / 255, opacity: 0.1)

    var body: some View {

        ZStack {
            self.content()
                .padding()
                .background(RoundedRectangle(cornerRadius: 22, style: .continuous)
                    .foregroundColor(.white)
                    .shadow(color: shadowColor, radius: 10, x: 0, y: 5)
            )
            }
            .aspectRatio(0.544, contentMode: .fit)
            .padding()
    }
}

struct MultipleChoiceOption : View {
    var option: String
    @State var isSelected: Bool

    var body: some View {
        ZStack {
            Rectangle()
                .foregroundColor(self.isSelected ? .gray : .white)
                .cornerRadius(6)
                .border(Color.gray, width: 1, cornerRadius: 6)
            Text(self.option)
                .font(.body)
                .foregroundColor(self.isSelected ? .white : .black)
                .padding()
                .multilineTextAlignment(.leading)
                .lineLimit(nil)
        }
    }
}

struct MultipleChoice : View {
    @State var selectedIndex = 1

    var options: [String] = [
        "Hello World",
        "How are you?",
        "This is a longer test This is a longer test This is a longer test This is a longer test This is a longer test This is a longer test"
    ]

    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                VStack(alignment: .leading, spacing: 12) {
                    ForEach(self.options.indices) { i in
                        MultipleChoiceOption(option: self.options[i],
                                             isSelected: i == self.selectedIndex)
                            .tapAction { self.selectedIndex = i }


                    }
                }
                    .frame(width: geometry.size.width)
            }
        }
            .padding()
    }
}

struct MultipleChoiceCard : View {
    var question: String = "Is this a question?"

    var body: some View {
        Card {
            VStack(spacing: 30) {
                Text(self.question)
                MultipleChoice()
            }
        }
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
//        NavigationView {
            VStack {
                MultipleChoiceCard()
                Button(action: {

                }) {
                    Text("Next")
                        .padding()
                        .foregroundColor(.white)
                        .background(Color.orange)
                        .cornerRadius(10)
                }
            }
                .padding()
//                .navigationBarTitle(Text("Hello"))
//        }
    }
}
#endif
like image 776
jjatie Avatar asked Jun 14 '19 13:06

jjatie


3 Answers

Please see this answer for Xcode 11 GM:

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

Summary: add .fixedSize(horizontal: false, vertical: true) — this worked for me in my use case.

like image 83
Wil Shipley Avatar answered Nov 13 '22 06:11

Wil Shipley


The modifier fixedSize() prevents the truncation of multiline text.

Inside HStack

Text("text").fixedSize(horizontal: false, vertical: true)

Inside VStack

Text("text").fixedSize(horizontal: true, vertical: false)
like image 31
zdravko zdravkin Avatar answered Nov 13 '22 06:11

zdravko zdravkin


There is currently a bug in SwiftUI causing the nil lineLimit to not work.

If you MUST fix this now, you can wrap a UITextField: https://icalvin.dev/post/403

like image 32
Marius Waldal Avatar answered Nov 13 '22 08:11

Marius Waldal