Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you rotate Text, Button, Rectangle in SwiftUI?

Tags:

ios

swiftui

How can you rotate Text, Button and other design controlls of SwiftUI by 90 degrees using SwiftUI?

I am intentionally modeling my question as a SwiftUI version of this one: How can you rotate text for UIButton and UILabel in Swift?

Current output:

Current output

Expected output:

enter image description here

like image 792
Hitesh Surani Avatar asked Jun 07 '19 12:06

Hitesh Surani


People also ask

How do I rotate a rectangle in SwiftUI?

Use any one of the . rotationEffect() methods to rotate any View clockwise, including Button and Text . Use the overloaded method with an anchor argument to rotate around a different point.

How do I rotate an object in SwiftUI?

Go to the preview pane and select the live view button. Drag the slider to rotate the image.

How do I rotate a view in SwiftUI?

SwiftUI's rotation3DEffect() modifier lets us rotate views in 3D space to create beautiful effects in almost no code. It accepts two parameters: what angle to rotate (in degrees or radians), plus a tuple containing the X, Y, and Z axis around which to perform the rotation.


1 Answers

Use any one of the .rotationEffect() methods to rotate any View clockwise, including Button and Text.

For example, this rotates Text about its origin (the center of its frame):

Text("Turtle Rock")
.rotationEffect(Angle(degrees: 90)))

Use the overloaded method with an anchor argument to rotate around a different point.

For example, this rotates Text about the bottom left point of its frame:

Text("Turtle Rock")
.rotationEffect(Angle(degrees: 90), anchor: .bottomLeading)

You can also use radians for rotation:

Text("Turtle Rock")
.rotationEffect(radians: Double.pi / 2)
like image 150
RPatel99 Avatar answered Nov 11 '22 02:11

RPatel99