SwiftUI - Align Text in a VStack

3k views Asked by At

How to align a Text with left alignment in a VStack that is occupying more space like this

This is the code am using

VStack(alignment:.leading) {
    Text("Test")
       .font(.system(size: 10))
       .frame(maxWidth: .infinity, maxHeight: .infinity)
}.frame(maxWidth: .infinity, maxHeight: .infinity)
 .background(Color.green)
2

There are 2 answers

1
Jeshurun Roach On

Try using this modifier on your Text view: .multilineTextAlignment(.leading)

1
Asperi On

You need to add alignment to Text frame, like

VStack(alignment:.leading) {
    Text("Test")
       .font(.system(size: 10))
       .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) // << here !!
}.frame(maxWidth: .infinity, maxHeight: .infinity)
 .background(Color.green)