SwiftUI多行文本始终在1行处截断

时间:2019-06-14 13:37:06

标签: swiftui

我正在尝试创建选项列表供用户选择。调试预览显示了总体外观。我的问题是在nil中将.lineLimit传递到MultipleChoiceOption不允许文本超过1行。我该如何纠正?

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

4 个答案:

答案 0 :(得分:14)

在修饰符下方添加,这样就不会复制Text(“ text”)。

用于HStack中的文本

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

如果VStack中的文本添加

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

答案 1 :(得分:8)

对于Xcode 11 GM,请参见以下答案:

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

摘要:添加.fixedSize(horizontal: false, vertical: true) —在我的用例中,这对我有用。

答案 2 :(得分:3)

SwiftUI中当前存在一个错误,导致nil lineLimit无法正常工作。

如果必须现在解决此问题,则可以包装UITextField: https://icalvin.dev/post/403

答案 3 :(得分:0)

我遇到了同样的问题,并使用了以下解决方法:

添加修饰符: .frame(idealHeight: .infinity)