16

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

Jared_M
  • 547
  • 1
  • 5
  • 13
  • Possible duplicate of [SwiftUI text-alignment](https://stackoverflow.com/questions/56443535/swiftui-text-alignment) – RPatel99 Jun 11 '19 at 11:31
  • The `Text` element automatically center the content, to handle alignment you should wrap it inside a Stack (`HStack` or `VStack`). – rraphael Jun 11 '19 at 11:34

1 Answers1

43

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

This works fine for me:

Text("[...]")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)
rraphael
  • 10,041
  • 2
  • 25
  • 33