【问题标题】:Get width of Text in SwiftUI在 SwiftUI 中获取文本的宽度
【发布时间】:2019-06-10 20:57:06
【问题描述】:

如何获取Text 组件的width

我想让我的Button 位于Text 下,与Text 本身的宽度相同。

VStack(alignment: .center, spacing: 20) {
      Text("Finde heraus, was gerade in der Welt los ist.")
           .font(.largeTitle).bold().lineLimit(3).multilineTextAlignment(.leading)

      Button(action: {

      }, label: {
           Text("Account erstellen").bold()
               .frame(minWidth: 0, maxWidth: .infinity)
               .frame(height: 40).padding(.top, 0)
               .background(Color.blue)
               .foregroundColor(.white)
               .cornerRadius(20)
      })
}.padding([.leading, .trailing], 25)

这给了我这个结果,在我看来它很丑:

在 iPad 上,文本和按钮之间的宽度更大

我该如何解决这个问题。

提前致谢。

【问题讨论】:

    标签: ios swift iphone xcode swiftui


    【解决方案1】:

    bold() 属性中似乎存在错误。删除它会导致文本按照您的预期对齐。您可以尝试提交一个错误,或者等待它,看看它是否在测试期间得到修复。

            Text("Finde heraus, was gerade in der Welt los ist.")
                .font(.largeTitle)
                .lineLimit(3)
                .multilineTextAlignment(.leading)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 2019-12-25
      • 2019-12-10
      • 2020-01-03
      • 1970-01-01
      • 2012-08-11
      • 2014-02-16
      相关资源
      最近更新 更多