17

I am trying to set alignment on my text inside of the Text struct but I can't find any property that does that. Code example (Updated):

VStack(alignment: .center) {
            Text(optionItem.title)
                .fontWeight(.heavy)
                .color(optionItem.color)
                .font(.system(size: 64))
                .frame(width: bounds.width, height: 100, alignment: .center)
                .padding(.top, 60)

            Text(optionItem.description)
                .lineLimit(nil)
                .padding([.leading, .trailing], 40)
                .frame(width: bounds.width, height: 120, alignment: .center)
                .font(.system(size: 16))

        }
        .padding(.bottom, bounds.height * 0.55)

The Text "object" is centered but not the text inside. Image:

preview

2
  • Possible duplicate of SwiftUI text-alignment Commented Jun 11, 2019 at 11:31
  • The Text element automatically center the content, to handle alignment you should wrap it inside a Stack (HStack or VStack). Commented Jun 11, 2019 at 11:34

1 Answer 1

46

You should use the .multilineTextAlignment(_:) method on the Text element.

This works fine for me:

Text("[...]")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)
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.