SwiftUI:文本框被截断

4

我的SwiftUI应用程序上的textField被截断了。但并不是每次都发生。它似乎是随机发生的。

应用程序屏幕截图显示文本被截断

以下是我正在使用的代码:

var body: some View {
    VStack {
      Spacer()
      // Target row
      HStack {
        Text("Put the bullseye as close as you can to:")
        Text("\(target)")
      }
      Spacer()
      // Slider row
      HStack {
        Text("1")
        Slider(value: $sliderValue, in: 1...100) {_ in
          print(self.sliderValue)
        }
        Text("100")
      }
      Spacer()
      // Hit me button row
      Button(action: {
        print("Button pressed")
        self.alertIsVisible = true
      }) {
        Text(/*@START_MENU_TOKEN@*/"Hit Me!"/*@END_MENU_TOKEN@*/)

      }
      .alert(isPresented: $alertIsVisible) { () -> Alert in
        let roundedValue = Int(sliderValue.rounded())
        let score = pointsForCurrentRound()
        return Alert(title: Text("Hello there!"), message: Text("The slider's value is \(roundedValue)!\n" +
          "You scored \(score) points this round"
          ), dismissButton: .default(Text("Awesome")))
      }
      Spacer()
      // Score and start over button row
      HStack {
        Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
          Text("Start Over")
        }
        Spacer()
        Text("Score:")
        Text("999999")
        Spacer()
        Text("Round:")
        Text("999")
        Spacer()
        Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
          Text("Info")
        }
      }
      .padding(.bottom, 20)
    }
  }

我尝试在文本字段后面添加填充和目标前面添加填充。我尝试在目标的前沿添加填充。我尝试使用文本字段上的帧方法添加最小长度。这些都没有起作用。有什么想法吗?

谢谢。


你使用的Xcode版本是哪个?我使用的是官方版的Xcode 11.1,在同一项目上进行了工作,并且已在iOS 13.1及以上版本中修复。 - Rudrank Riyam
谢谢回复。我正在使用最新版本的Xcode 11.2。iOS 13.1及以上版本有哪些修复? - JustMe
啊,所以在iOS 13.1及以上版本中,文本根据其大小占据整个空间,而不是截断。你能告诉我这种情况是随机发生的吗?是在预览中还是模拟器/真实设备中? - Rudrank Riyam
它在预览和模拟器中都会发生。我还没有在真实设备上尝试过。 - JustMe
1
我刚刚重新安装了11.1版本并尝试了这个应用。文本不再被截断。所以可能是11.2版本的问题。我会提交一个错误报告。谢谢你帮我解决了这个问题。 - JustMe
3个回答

20

您可以添加fixedSize()以锁定标签。

HStack {
  Text("Put the bullseye as close as you can to:").fixedSize()
  Text("\(target)").fixedSize()
}

4

我刚遇到了这个完全一样的问题!在搜索、尝试和错误几分钟后,我终于弄清楚了。文本视图正在尝试调整大小,但其中一个父视图启用了动画效果。如果任何遇到同样问题的人向 Text 添加 .animation(nil),这可能会解决问题。

VStack {
    Text("\(Int(self.viewModel.ProgressPercentage * 100.0))%")
        .font(.largeTitle)
        .animation(nil)
}

祝你好运!


2
这对我起作用了
Text("hello!")
    .fixedSize(horizontal: true, vertical: false)

文本可以水平扩展,而不是垂直扩展。

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