SwiftUI多行文字始终在一行截断

SwiftUI多行文字始终在一行截断,swiftui,Swiftui,我正在尝试创建一个选项列表,供用户选择。调试预览显示了一般的外观。我的问题是将nil传递到.lineLimit中的multipleechoiceoption不允许文本超过一行。我怎样才能纠正这个问题 struct Card<Content: View> : View { private let content: () -> Content init(content: @escaping () -> Content) { self.conte

我正在尝试创建一个选项列表,供用户选择。调试预览显示了一般的外观。我的问题是将
nil
传递到
.lineLimit
中的
multipleechoiceoption
不允许文本超过一行。我怎样才能纠正这个问题

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
结构卡:视图{ 私有出租内容:()->内容 init(内容:@escaping()->content){ self.content=内容 } 私有let shadowColor=Color(红色:69/255,绿色:81/255,蓝色:84/255,不透明度:0.1) var body:一些观点{ ZStack{ self.content() .padding() .background(圆角半径:22,样式:连续) .foregroundColor(.白色) .阴影(颜色:阴影颜色,半径:10,x:0,y:5) ) } .aspectRatio(0.544,contentMode:.fit) .padding() } } 结构多重切面选项:视图{ 变量选项:字符串 @州:布尔 var body:一些观点{ ZStack{ 矩形() .foregroundColor(自选?.gray:.白色) .转弯半径(6) .边框(颜色.灰色,宽度:1,角半径:6) 文本(self.option) .font(.body) .foregroundColor(自选?.white:.黑色) .padding() .multilitextalignment(.leading) .lineLimit(无) } } } 结构多重切面:视图{ @状态变量selectedIndex=1 变量选项:[字符串]=[ “你好,世界”, “你好吗?”, “这是一个较长的测试这是一个较长的测试这是一个较长的测试这是一个较长的测试这是一个较长的测试” ] var body:一些观点{ GeometryReader{中的几何体 滚动视图{ VStack(对齐:。前导,间距:12){ ForEach(self.options.index){i in MultipleChiceOption(选项:self.options[i], isSelected:i==self.selectedIndex) .tapAction{self.selectedIndex=i} } } .框架(宽度:几何尺寸宽度) } } .padding() } } 结构多重IC卡:视图{ var question:String=“这是一个问题吗?” var body:一些观点{ 卡片{ VStack(间距:30){ 文本(自我提问) 多重回声 } } } } #如果调试 结构内容视图\u预览:PreviewProvider{ 静态var预览:一些视图{ //导航视图{ VStack{ 多回传IC卡() 按钮(操作:{ }) { 文本(“下一页”) .padding() .foregroundColor(.白色) .背景(颜色.橙色) .转弯半径(10) } } .padding() //.navigationBarTitle(文本(“Hello”)) // } } } #恩迪夫
目前SwiftUI中存在一个错误,导致nil lineLimit无法工作

如果必须立即解决此问题,可以包装UITextField:

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

添加修改器:
.frame(理想高度:无穷大)
请查看Xcode 11 GM的以下答案:

小结:添加
.fixedSize(水平:false,垂直:true)
-这在我的用例中对我有效。

在下面添加修饰符,这样文本(“文本”)就不会被分割

对于HStack中的文本

Text("text").fixedSize(horizontal: false, vertical: true)
如果VStack中有文本,则添加

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

它并不总是一个bug。重新排序和重新组织视图的布局可以解决问题,但这通常并不理想。恐怕这个解决方案会崩溃。