0

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.

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.