0

如何进行文本换行?这是我的代码:

struct ContentView: View {
    private let items: [String] = [
        "OneLineLongggggggggggggggggggggggggggggggggggggggggg",
        "TwoLinesLonggggggggggggg\nLongggggggggggggggg",
        "ThreeLinesLonggggggggggggg\nLongggggggggggggggg\nLongggggggggggggggg"
    ]
    @State private var text: String = ""
    var body: some View {
        VStack {
            Picker("Select Text", selection: self.$text) {
                ForEach(self.items, id: \.self) {
                    Text($0)
                        .tag($0)
                }
            }
            Text("select: \(self.text)")
        }
    }
}

结果如下: 在此处输入图像描述

如果我添加 fixSize 然后元素相互运行:

struct ContentView: View {
    private let items: [String] = [
        "OneLineLongggggggggggggggggggggggggggggggggggggggggg",
        "TwoLinesLonggggggggggggg\nLongggggggggggggggg",
        "ThreeLinesLonggggggggggggg\nLongggggggggggggggg\nLongggggggggggggggg"
    ]
    @State private var text: String = ""
    var body: some View {
        VStack {
            Picker("Select Text", selection: self.$text) {
                ForEach(self.items, id: \.self) {
                    Text($0)
                        .tag($0)
                        .fixedSize(horizontal: false, vertical: true)
                }
            }
            Text("select: \(self.text)")
        }
    }
}

结果如下: 在此处输入图像描述

请告诉我从哪个方向寻找答案?

4

1 回答 1

0

请在文本中使用 .lineLimit(您想要的任意数量的最大行数)。

在这种情况下,您可以使用

 Text($0)
    .tag($0)
    .lineLimit(3)
于 2021-06-01T08:21:18.723 回答