SwiftiSwift
SwiftiSwift

Reputation: 8787

Get width of Text in SwiftUI

how can I get the width of a Text component?

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:

enter image description here

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

How can I solve this issue.

Thanks In advance.

Upvotes: 0

Views: 1568

Answers (1)

DLAN
DLAN

Reputation: 620

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 aligned with button width

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

Upvotes: 1

Related Questions