firstTextBsaeline alignment not working in LazyHStack

91 Views Asked by At

I'm trying to align the first line of texts with .firstTextBaseline however doesn't seem to work with LazyHStack. Similarly using AlignmentGuide also doesn't seem to be working. Am i missing something with understanding LazyHStacks?

HStack LazyHStack
enter image description here enter image description here

Sample code:

struct ContentView: View {
var body: some View {
        LazyHStack(alignment: .firstTextBaseline) {
            VStack {
                Color.green
                    .frame(width: 100, height: 100)
                Text("blah blah")
            }
//                .alignmentGuide(VerticalAlignment.firstTextBaseline) { d in  // this also has no effect
//                    d[.top]
//                }

            Text("---- Center Line ----")


            VStack {
                Color.green
                    .frame(width: 100, height: 100)
                Text("blah blah blah \nblah")
                    .fixedSize()
            }

        }
        .background {
            Color.blue
        }
    }
}
1

There are 1 best solutions below

0
bennyyy999 On

What you could do, which seems more of a hack, than a real solution, is to use it as an HStack, but put everything inside a LazyVStack like so:

LazyVStack {
    HStack(alignment: .firstTextBaseline) {
        VStack {
            Color.green
                .frame(width: 100, height: 100)
             Text("blah blah")
        }
                
                
        Text("---- Center Line ----")
                
                
        VStack {
            Color.green
                .frame(width: 100, height: 100)
            Text("blah blah blah \nblah")
                .fixedSize()
        }
                
    }
    .background {
        Color.blue
    }
}