44

I'm attempting to rotate a UILabel 90 degrees within a UIView.

The following snippet is an attempt to write something in Swift that can do that:

  //  #define DEGREES_TO_RADIANS(x) (x * M_PI/180.0)

    let angle:CGFloat = (90.0 * 3.14/180.0) as CGFloat
    let rotation = CGAffineTransformMakeRotation(angle)

    self.dayLabel.transform(rotation)

I encountered couple compiler errors:
1) Attempting to convert radians to an angle encountered a double --> float conversion problem and hence, had to use '3.14' vs 'M_Pi'.
2) I got the following error when attempting to actually doing the transformation on UILabel: enter image description here

What's the correct way to rotate a UILabel 90 degrees?


Okay... I got the transform syntax but...
Here's the original image:
enter image description here

My goal is to position 'TUE' vertically against the left side of the cell.
I originally tried a 90 deg counter rotation:

    self.dayLabel.transform = CGAffineTransformMakeRotation(-90)

And got the following:
enter image description here

I tried to vary the degrees but can't get a solid vertical position.
I do/can I have tighter control over the transformation?

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105

10 Answers10

70

Below code works in Swift 3, 4 and 5:

dayLabel.transform = CGAffineTransform(rotationAngle: .pi/2)
Mohit Kumar
  • 2,898
  • 3
  • 21
  • 34
Mette
  • 1,166
  • 11
  • 12
21

Try this way :

dayLabel.transform = CGAffineTransform(rotationAngle: .pi/2)
Matthew Usdin
  • 1,264
  • 1
  • 19
  • 20
8
self.dayLabel.transform = CGAffineTransformMakeRotation(M_PI_2)

Is the correct way to perform the transformation of 90 degrees, where M_PI_2 is the predefined radian value for 90 degrees.

Myxtic
  • 5,679
  • 5
  • 48
  • 69
chris
  • 4,840
  • 5
  • 35
  • 66
8

Swift 4 solution with degrees value:

let degrees : Double = -90; //the value in degrees
self.dayLabel.transform = CGAffineTransform(rotationAngle: CGFloat(degrees * .pi/180))
Daphyduck
  • 127
  • 1
  • 10
8

swift 4

@IBOutlet weak var ourView: UIView!

    ourView.transform = CGAffineTransform(rotationAngle: CGFloat(Double(-45) * .pi/180))
//i gave it -45 degree.

enter image description here

Akbar Khan
  • 2,215
  • 19
  • 27
2

UIView.transform is a property of the UIView not a function.
Try setting self.label.transform = CGAffineTransformMakeRotation(angle) instead.

For more information https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/transform

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
1

To ease calculation between radians and degrees forth and back I would even suggest to use the measurement API, which offers such built in:

let measurement = Measurement<UnitAngle>(value: 90.0, unit: .degrees)
let angleInRadians = measurement.converted(to: .radians).value
label.transform = CGAffineTransform(rotationAngle: angleInRadians)
0
dayLabel.transform = dayLabel.transform.rotated(by: .pi / 2)
Booharin
  • 753
  • 10
  • 10
0
func rotate(button: UIButton) {
let angles = [CGFloat(Double(270) * .pi/180),
              CGFloat(Double(180) * .pi/180),
              CGFloat(Double(90) * .pi/180),
              CGFloat(Double(0) * .pi/180)
]
for i in 0..<4 {
    UIView.animate(withDuration: 0.125, delay: 0.125 * Double(i), animations: {
        button.transform = CGAffineTransform(rotationAngle: angles[i])
    }, completion: nil)
}
Stotch
  • 373
  • 3
  • 10
-8

Apparently I have adjust down to the 1/100 of a counter degree to nudge it vertical:

self.dayLabel.transform = CGAffineTransformMakeRotation(-89.55)

The result:
enter image description here

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • 15
    It is clear from the documentation that `CGAffineTransformMakeRotation` gets an angle in radians as a parameter, you just happened to find an angle which looked quite similar to what you wanted. – DeFrenZ Feb 04 '15 at 12:39