SwiftUI多行文本总是在一行截断

42
我试图创建一个选项列表供用户选择。调试预览显示了一般的外观和感觉。我的问题是,在MultipleChoiceOption中将nil传递给.lineLimit不允许文本超过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
5个回答

69

39

fixedSize()修饰符可以防止多行文本被截断。

在HStack内部

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

VStack内部

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

3
这是相反的。 - Marcus Ziadé
1
我修复了相反的逻辑。现在应该没问题了。 - Trev14

2
目前 SwiftUI 存在一个 bug,导致 nil 的 lineLimit 无法正常工作。如果你必须立即修复此问题,可以使用 UITextField 进行包装: https://icalvin.dev/post/403。将此链接复制到浏览器中以获取更多信息。

4
不是每次都是程序漏洞。重新排列和组织视图的布局可以解决问题,但通常这并不理想。 - RPatel99

0
在我的情况下,Text 在顶部有一个填充,当键盘没有出现时,它看起来很好,但是当键盘出现时,多行文本变成了单行,只有部分内容可见。
将视图包裹在ScrollView中解决了这个问题,因为当键盘出现在屏幕上时,视图的高度会减小,所以Text组件无法找到足够的空间来显示其内容,ScrollView使Text能够以多行显示整个内容。

-6
我遇到同样的问题,采用了以下解决方法:
添加修饰符:.frame(idealHeight: .infinity)

1
恐怕这个解决方案对我来说会崩溃。 - Daniel Ryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接