Reputation: 2229
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:
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
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))
}
}
}
}
Upvotes: 2
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)
}
}
}
Upvotes: 1
Reputation: 8091
VStack(alignment: .trailing) {
Text("Text 1")
Text("Text 2")
Text("Text 34")
}.rotationEffect(.degrees(-90))
Upvotes: 4