【问题标题】:SwiftUI List containing Text with word wrap on MacSwiftUI 列表包含 Mac 上自动换行的文本
【发布时间】:2020-02-27 15:54:32
【问题描述】:

我在 Mac 上遇到了包含文本行的列表的问题。

代码如下:

struct ContentView: View {
    var messages = [
        "This is a long piece of text that's going to need to be wrapped to fit into the view.",
        "This is another long piece of text that's going to need to be wrapped to fit into the view."
    ]

    var body: some View {
        List(messages, id: \.self) { message in
            Text(message)
                .lineLimit(nil)
                .border(Color.blue)
                .fixedSize(horizontal: false, vertical: true)

                // Also tried .fixedSize(horizontal: true, vertical: false)
                // which seems more correct, but doesn't wrap the text at all
        }.frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

当我在 iOS 上运行它时,结果是我所期望的:

然而,在 Mac 上,SwiftUI 正在创建的基础表似乎没有调整行高以适应包装的内容:

我已将此报告为错误 (FB7421021),但在这里询问以防其他人遇到此问题。

谢谢

【问题讨论】:

  • 您是否尝试将列表放入滚动视图中?
  • 我做到了..这让文本对我来说完全消失了。

标签: macos swiftui


【解决方案1】:

这是从对我有用的类似用例中采用的方法。希望对其他人有用。

struct TestWrappedText: View {
    var messages = [
        "This is a long piece of text that's going to need to be wrapped to fit into the view.",
        "This is another long piece of text that's going to need to be wrapped to fit into the view."
    ]

    @State var alignedHeight: [CGFloat] = [0, 0]

    var body: some View {
        List(0 ..< messages.count) { i in
            Text(self.messages[i])
                .border(Color.blue)
                .fixedSize(horizontal: false, vertical: true)
                .modifier(ViewHeightKey())
                .onPreferenceChange(ViewHeightKey.self) {
                    if self.alignedHeight[i] != $0 {
                        self.alignedHeight[i] = $0
                    }
                }
                .frame(height: self.alignedHeight[i])
        }
    }
}

struct ViewHeightKey: PreferenceKey {
    static var defaultValue: CGFloat { 0 }
    static func reduce(value: inout Value, nextValue: () -> Value) {
        value = nextValue()
    }
}

extension ViewHeightKey: ViewModifier {
    func body(content: Content) -> some View {
        return content.background(GeometryReader { proxy in
            Color.clear.preference(key: Self.self, value: proxy.size.height)
        })
    }
}

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多