ios - 在 SwiftUI 中获取文本的宽度

标签 ios swift iphone xcode swiftui

如何获取 Text 组件的 width

我想让 Text 下的 ButtonText 本身的宽度相同。

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)

这给了我这个结果,在我看来它非常丑陋:

enter image description here

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

我该如何解决这个问题。

提前致谢。

最佳答案

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

Text aligned with button width

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

关于ios - 在 SwiftUI 中获取文本的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56533428/

相关文章:

ios - -(无效)scrollViewDidScroll :(UIScrollView *)scrollView too slow in iOS 8

iphone - 在 iPhone 模拟器中录制音频时出错

ios - 即使排列的 subview 被隐藏,UIStackView 似乎也会堆叠间距

Swift - 在两个 TableView (2 个 View Controller )之间传递数据

ios - 扩展 UITableViewCell 重叠

ios - 使用 UIActivityViewController 共享图像

swift - SKAction 序列不会延迟节点的生成

iphone - UIImageView - 显示目录中的图像

iphone - Safari 中的 UIWebView 打开链接不起作用?

ios - OCMock 部分模拟拆卸问题