Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: Text has not the full width of screen after rotation

Tags:

swiftui

The app only supports the potrait mode. Two questions should be displayed, but they are rotated accordingly. So the players can see the questions well, no matter where they sit.

Anyway, it seems that the bounding box is not rotated while rotating.

ZStack {
        HStack {
            GeometryReader { proxy in
                Text(challenge)
                    .rotationEffect(Angle(degrees: 90), anchor: .center)
                    .frame(maxWidth: proxy.size.height, minHeight: proxy.size.width)
                    .foregroundColor(.red)
                    .font(.largeTitle)
                    .background(Color.blue)
            }
            Spacer()
            GeometryReader { proxy in
                Text(challenge)
                    .rotationEffect(Angle(degrees: 270), anchor: .center)
                    .frame(maxWidth: proxy.size.height, minHeight: proxy.size.width)
                    .foregroundColor(.red)
                    .font(.largeTitle)
                    .background(Color.blue)
            }
        }
    }

I have tried many different things. I left out the GeometryReader and worked with 'fixedSize()`. Then I get a one-liner that goes across the screen.

I have also tried this solution, but it does not work as expected.

My result is always a text which does not use the full width. (The overlapping is just another mistake, but I will definitely get that under control).

enter image description here

What I actually want to have: enter image description here

like image 301
kuzdu Avatar asked Oct 25 '25 05:10

kuzdu


1 Answers

Here is a demo of possible approach. Prepared with Xcode 12.1 / iOS 14.1

demo

struct DemoView: View {
    var body: some View {
        HStack {
            RotatedText(text: lorem, angle: Angle(degrees: 90))
            RotatedText(text: lorem, angle: Angle(degrees: -90))
        }
    }
}

struct RotatedText: View {
    let text: String
    let angle: Angle
    var color: Color = .blue
    
    var body: some View {
        color.overlay(
            GeometryReader { gp in
                VStack {
                    Text(text)
                        .frame(width: gp.size.height, height: gp.size.width)
                        .rotationEffect(angle)
                }.frame(width: gp.size.width, height: gp.size.height)
            }
        )
    }
}
like image 50
Asperi Avatar answered Oct 27 '25 00:10

Asperi