Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/28312638/whats-the-correct-way-to-rotate-a-uilabel-90-degrees

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Below code works in Swift 3, 4 and 5:

dayLabel.transform = CGAffineTransform(rotationAngle: .pi/2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...