【问题标题】:The truncation problem of SwiftUI Text ViewSwiftUI Text View 的截断问题
【发布时间】:2019-11-14 02:47:33
【问题描述】:

我正在制作一个 swiftUI 日历,遇到了一个奇怪的 SwiftUI 文本视图截断问题。

struct test : View {
var body: some View {
    GeometryReader { geometry in
        HStack(alignment: .center, spacing: 0) {
            ForEach(0..<7) { _ in
                Text("Tue").frame(width: geometry.size.width / 7).border(Color.red, width: 2)
            }               
        }
    }
}
}

一开始我以为可能是因为Text View size不够大。但是当我减小文本视图宽度时,截断就消失了。我还尝试设置较小的字体,但它也不起作用。感谢您的任何提示!

struct test : View {
var body: some View {
    GeometryReader { geometry in
        HStack(alignment: .center, spacing: 0) {
            ForEach(0..<7) { _ in
                Text("Tue").frame(width: geometry.size.width / 8).border(Color.red, width: 2)
            }
        }
    }
}
}

【问题讨论】:

    标签: swiftui truncation


    【解决方案1】:

    测试版 5

    在 beta 5 中,问题似乎已经解决。


    Beta 4 及以前的版本

    这绝对是只在 iPhone XS 中出现的错误,而在 iPhone XR 中则没有。请注意,XS 为 375 点宽,而 XR 为 414 点。然而,这与它无关。 375 足以容纳 7 个标签。

    我认为您应该提交一份错误报告,同时使用 XR 进行开发。 如果有人有实际设备,很高兴知道那里是否也存在该错误

    我创建了这个小例子,展示了它在 Xs 上的运行异常

    这是 Xr,它应该可以正常工作:

    示例代码如下:

    struct ContentView: View {
        @State private var slider: Float = 100.0
    
        var body: some View {
            VStack {
                GeometryReader { geometry in
                    HStack(alignment: .center, spacing: 0) {
                        ForEach(0..<7) { _ in
                            Text("Tue").frame(width: geometry.size.width / 7, height: 30).border(Color.blue)
                        }
                    }
                }.frame(width: Length(slider), height: 40)
                Text("\(slider)")
                Slider(value: self.$slider, from: 100.0, through: 375.0, by: 1.0)
                Spacer()
            }
        }
    }
    

    【讨论】:

    • 您是否在预览设置中使用了任何修饰符?如果有,是哪一个?
    • 还要注意,如果你想阻止省略号,你可以使用 fiexedSize()。文本将根据需要扩展到其边界之外:Text("Tuesday").fixedSize().frame(width: geometry.size.width / 7).
    • 非常感谢!我选择的是 iPhone Xs。我将其更改为 iPhone XR,现在一切正常。
    • @echo 我更新了答案以显示它实际上是一个错误。
    • 感谢您的更新! @kontiki 因为一切仍然是 Beta 版本,所以我可以使用 iPhone XR 模拟器。希望在未来的新版本中修复此错误。
    猜你喜欢
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2013-12-13
    • 1970-01-01
    • 2020-08-03
    • 2010-09-12
    • 2012-11-21
    相关资源
    最近更新 更多