在SwiftUI中获取文本的宽度 [英] Get width of Text in SwiftUI

查看:92
本文介绍了在SwiftUI中获取文本的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 Text 组件的 width ?

我想将我的 Button 设置在 Text 下,其宽度与 Text 本身相同.

I want to make my Button that is under the Text, the same width as the Text itself.

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)

哪个给我这个结果,我认为这很丑陋:

Which is giving me this result, its pretty ugly in my opinion:

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

On the iPad the width between the text and the button is even bigger

我该如何解决这个问题.

How can I solve this issue.

谢谢.

推荐答案

bold()属性似乎存在错误.删除它会导致文本按预期对齐.您可以尝试提交错误,也可以等一下,看看它是否在Beta版期间得到修复.

It seems there is a bug in the bold() attribute. Removing it caused the text to align as you are expecting. You can try filing a bug, or wait it out and see if it gets fixed over the course of the beta period.

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

这篇关于在SwiftUI中获取文本的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆