将 UILabel 旋转 90 度的正确方法是什么? [英] What's the correct way to rotate a UILabel 90 degrees?

查看:40
本文介绍了将 UILabel 旋转 90 度的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 UIView 中将 UILabel 旋转 90 度.

下面的代码片段试图用 Swift 编写一些可以做到这一点的东西:

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)

我遇到了几个编译器错误:
1) 尝试将弧度转换为角度时遇到了 double --> float 转换问题,因此,必须使用3.14"与M_Pi".
2) 尝试在 UILabel 上实际进行转换时出现以下错误:

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:

将 UILabel 旋转 90 度的正确方法是什么?


好的...我得到了转换语法但是...
这是原图:

我的目标是将TUE"垂直放置在单元格的左侧.
我最初尝试了 90 度反向旋转:

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


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

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)

并得到以下内容:

我试图改变度数,但无法获得稳固的垂直位置.
我可以/我可以更严格控制转换吗?

推荐答案

以下代码适用于 Swift 3、4 和 5:

Below code works in Swift 3, 4 and 5:

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

这篇关于将 UILabel 旋转 90 度的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆