1

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)
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
Joe
  • 11
  • 1
  • 2

2 Answers2

3

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)
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thank you. Got the same from this question as well: https://stackoverflow.com/questions/60800624/how-to-keep-text-as-leading-alignment-after-set-text-framemaxwidth-infinity – Joe Oct 04 '20 at 07:10
2

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

  • Helpful if you have longer text which has an automatic linebreak into multiple lines. – Jan Mar 15 '23 at 15:29