Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

rotation

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?

like image 394
Frederick C. Lee Avatar asked Feb 04 '15 02:02

Frederick C. Lee


People also ask

What is the rule for rotating 90 degrees clockwise?

Here are the rotation rules: 90° clockwise rotation: (x,y) becomes (y,-x) 90° counterclockwise rotation: (x,y) becomes (-y,x) 180° clockwise and counterclockwise rotation: (x, y) becomes (-x,-y)


2 Answers

Below code works in Swift 3, 4 and 5:

dayLabel.transform = CGAffineTransform(rotationAngle: .pi/2) 
like image 146
Mette Avatar answered Sep 28 '22 09:09

Mette


Try this way :

dayLabel.transform = CGAffineTransform(rotationAngle: .pi/2) 
like image 45
mattyU Avatar answered Sep 28 '22 09:09

mattyU