Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center alignment text inside of Text in SwiftUI

Tags:

swift

swiftui

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

like image 998
John Doe Avatar asked Jun 11 '19 11:06

John Doe


1 Answers

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

This works fine for me:

Text("[...]")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)
like image 152
rraphael Avatar answered Oct 18 '22 02:10

rraphael