Christopher Graf
Christopher Graf

Reputation: 2229

Align rotated Texts in SwiftUI HStack

I am trying to trailing align rotated texts (top aligned after rotation?). I have tried different combinations of the HStack alignment parameter, each Text's alignmentGuide() ViewModifier and a custom VerticalAlignmentGuide but was not able to achieve the desired result. I did refrain from using the .frame() ViewModifier since the text's scale factor might change depending on the amount of Text Views.

Needed alignment shown with red line:

image with alignment description

Base code:

HStack {
    VStack {
        Spacer()
        Rectangle()
            .fill(Color.green)
            .frame(height: 80)
        Text("Text 1")
            .rotationEffect(.degrees(-90))
            .padding(.top, 30)
    }
    VStack {
        Spacer()
        Rectangle()
            .fill(Color.green)
            .frame(height: 80)
        Text("Text 2")
            .rotationEffect(.degrees(-90))
            .padding(.top, 30)
    }
    VStack {
        Spacer()
        Rectangle()
            .fill(Color.green)
            .frame(height: 80)
        Text("Text 34")
            .rotationEffect(.degrees(-90))
            .padding(.top, 30)
    }
}

Upvotes: 3

Views: 1196

Answers (3)

Chris
Chris

Reputation: 8091

ok, my last one now, hope you are satisfied ;)

struct ContentView: View {
    var body: some View {
        HStack {
            VStack(alignment: .center) {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 80)
                Text("Text 1")
                    .frame(width:100, height:100, alignment: .trailing)
                    .rotationEffect(.degrees(-90))
            }
            VStack(alignment: .center) {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 80)
                Text("Text 2")
                    .frame(width:100, height:100, alignment: .trailing)
                    .rotationEffect(.degrees(-90))
            }
            VStack(alignment: .center) {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 80)
                Text("Text 345")
                    .frame(width:100, height:100, alignment: .trailing)
                    .rotationEffect(.degrees(-90))
            }
        }
    }
}

enter image description here

Upvotes: 2

Chris
Chris

Reputation: 8091

new answer for your comment update:

HStack {
            VStack {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 80)
                Text("Text 1")
                    .rotationEffect(.degrees(-90), anchor: .bottomTrailing)
                    .padding(.top, 30)
            }
            VStack {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 80)
                Text("Text 2")
                    .rotationEffect(.degrees(-90), anchor: .bottomTrailing)
                    .padding(.top, 30)
            }
            VStack {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 80)
                Text("Text 34")
                    .rotationEffect(.degrees(-90), anchor: .bottomTrailing)
                    .padding(.top, 30)
            }
        }
    }

enter image description here

Upvotes: 1

Chris
Chris

Reputation: 8091

enter image description heretry this:

VStack(alignment: .trailing) {
            Text("Text 1")

            Text("Text 2")

            Text("Text 34")

        }.rotationEffect(.degrees(-90))

Upvotes: 4

Related Questions