首页 > 解决方案 > SwiftUI 文本行为

问题描述

我在我的设备上看到了一些在模拟器中没有看到的奇怪的文本字段行为。我在从 @ObservedObject 变量调用的 VStack 中有一个标准的文本字段分组,如下所示:

    @ObservedObject var timer: ActivityTimer

    var body: some View {
        VStack {
            VStack {
                Text(timer.currentCountdownString).font(Font.system(size: 90))
                Text(timer.currentActivityName).font(.largeTitle).bold().underline().padding(.bottom, 5)
                Text(timer.currentIncline).font(.title)
            }
            .padding()
            .cornerRadius(10)

        }

当变量发生变化时,我看到设备上的文本字段发生变化,但输出经常被截断为...,请参见下文。预先感谢您的协助。

正确的 不正确

标签: swiftswiftui

解决方案


我在旋转到横向时遇到了这个问题(即使有更多的水平空间。)我修复它的方法是调用.fixedSize().Text

Text(timer.currentActivityName)
  .font(.largeTitle)
  .bold()
  .underline()
  .fixedSize()
  .padding(.bottom, 5)

确保在设置权重、字体等之后但在填充之前进行 fixedSize 调用。


推荐阅读