Swift:如何为UIButton和UILabel旋转文本?

时间:2020-12-15 09:24:56

How can you rotate text for UIButton and UILabel? 90 degrees, 180 degrees

如何为UIButton和UILabel旋转文本?90度,180度

Note: Before you mark this as a duplicate, I am intentionally modelling my question as a Swift version of this one: Objective C: How can you rotate text for UIButton and UILabel?

注意:在您将其标记为副本之前,我有意将我的问题建模为这个问题的快速版本:Objective C:如何为UIButton和UILabel旋转文本?

4 个解决方案

#1


146  

I am putting my answer in a similar format to this answer.

我把我的答案和这个答案放在一个类似的格式。

Here is the original label:

这是最初的标签:

Swift:如何为UIButton和UILabel旋转文本?

Rotate 90 degrees clockwise:

顺时针旋转90度:

yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)

Swift:如何为UIButton和UILabel旋转文本?

Rotate 180 degrees:

180度旋转:

yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

Swift:如何为UIButton和UILabel旋转文本?

Rotate 90 degrees counterclockwise:

逆时针旋转90度:

yourLabelName.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)

Swift:如何为UIButton和UILabel旋转文本?

Do the same thing to rotate a button. Thankfully the touch events also get rotated so the button is still clickable in its new bounds without having to do anything extra.

做同样的事情旋转一个按钮。幸运的是,触摸事件也被旋转,所以按钮仍然可以在新的边界上点击,而不需要做任何额外的事情。

yourButtonName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)

Notes:

注:

Documentation for CGAffineTransform

文档CGAffineTransform

The basic format is CGAffineTransform(rotationAngle: CGFloat) where rotationAngle is in radians, not degrees.

基本的格式是CGAffineTransform(rotationAngle: CGFloat),其中rotationAngle是弧度,而不是角度。

There are 2π radians in a full circle (360 degrees). Swift includes the useful constant CGFloat.pi.

在一个完整的有2π弧度圆(360度)。Swift包含有用的常数CGFloat.pi。

  • CGFloat.pi = π = 180 degrees
  • CGFloat。π=π= 180度
  • CGFloat.pi / 2 = π/2 = 90 degrees
  • CGFloat。π/ 2 =π/ 2 = 90度

Auto Layout:

自动布局:

Auto layout does not work with rotated views. (See Frame vs Bounds for an explanation why.) This problem can be solved by creating a custom view. This answer shows how to do it for a UITextView, but it is the same basic concept for a label or button. (Note that you will have to remove the CGAffineTransformScale line in that answer since you don't need to mirror the text.)

自动布局不能与旋转视图一起工作。(请参阅框架vs界限来解释为什么。)这个问题可以通过创建自定义视图来解决。这个答案显示了如何对UITextView执行此操作,但它与标签或按钮的基本概念相同。(请注意,由于不需要镜像文本,所以必须删除该答案中的CGAffineTransformScale行。)

Related

相关的

#2


6  

swift

Rotate 90 degrees clockwise:

顺时针旋转90度:

    var rotateLabel: UILabel = UILabel(frame: CGRectMake(100, 0, 28, 159))

    rotateLabel.textAlignment = .Right

    rotateLabel.text = "Hello!"

    self.view.addSubview(rotateLabel)

    rotateLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

    rotateLabel.frame = CGRectMake(100, 0, 28, 159)

Rotate 90 degrees counterclockwise:

逆时针旋转90度:

    var rotateLabel: UILabel = UILabel(frame: CGRectMake(100, 0, 28, 159))

    rotateLabel.textAlignment = .Right

    rotateLabel.text = "Hello!"

    self.view.addSubview(rotateLabel)

    rotateLabel.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))

    rotateLabel.frame = CGRectMake(100, 0, 28, 159)

#3


1  

If you do this a lot, you'll wan't to make an extension. Also this will allow you to rotate your view 0-360 degrees.

如果你经常这样做,你就不会再做延期了。这也将允许你旋转你的视图0-360度。

extension UIView {
    func rotate(degrees: CGFloat) {
        rotate(radians: CGFloat.pi * degrees / 180.0)
    }

    func rotate(radians: CGFloat) {
        self.transform = CGAffineTransform(rotationAngle: radians)
    }
}

Then you can simply call rotate on your views

然后您可以简单地调用视图上的rotate

myView.rotate(degrees: 90)

#4


0  

Adapted the answer from Ananthu R Krishnan to fit into Swift 3.1.

改编了Ananthu R Krishnan的答案以适应Swift 3.1。

Rotate 90 degrees counterclockwise:

逆时针旋转90度:

let rotateLabel: UILabel = UILabel(frame: CGRect(x:15, y:66, width:28, height:159))
rotateLabel.textAlignment = .right
rotateLabel.text = "TEXT"
self.view.addSubview(rotateLabel)
rotateLabel.transform = CGAffineTransform(rotationAngle: CGFloat(-(Double.pi / 2.0)))
rotateLabel.frame = CGRect(x:15, y:66, width:28, height:159)

#1


146  

I am putting my answer in a similar format to this answer.

我把我的答案和这个答案放在一个类似的格式。

Here is the original label:

这是最初的标签:

Swift:如何为UIButton和UILabel旋转文本?

Rotate 90 degrees clockwise:

顺时针旋转90度:

yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)

Swift:如何为UIButton和UILabel旋转文本?

Rotate 180 degrees:

180度旋转:

yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

Swift:如何为UIButton和UILabel旋转文本?

Rotate 90 degrees counterclockwise:

逆时针旋转90度:

yourLabelName.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)

Swift:如何为UIButton和UILabel旋转文本?

Do the same thing to rotate a button. Thankfully the touch events also get rotated so the button is still clickable in its new bounds without having to do anything extra.

做同样的事情旋转一个按钮。幸运的是,触摸事件也被旋转,所以按钮仍然可以在新的边界上点击,而不需要做任何额外的事情。

yourButtonName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)

Notes:

注:

Documentation for CGAffineTransform

文档CGAffineTransform

The basic format is CGAffineTransform(rotationAngle: CGFloat) where rotationAngle is in radians, not degrees.

基本的格式是CGAffineTransform(rotationAngle: CGFloat),其中rotationAngle是弧度,而不是角度。

There are 2π radians in a full circle (360 degrees). Swift includes the useful constant CGFloat.pi.

在一个完整的有2π弧度圆(360度)。Swift包含有用的常数CGFloat.pi。

  • CGFloat.pi = π = 180 degrees
  • CGFloat。π=π= 180度
  • CGFloat.pi / 2 = π/2 = 90 degrees
  • CGFloat。π/ 2 =π/ 2 = 90度

Auto Layout:

自动布局:

Auto layout does not work with rotated views. (See Frame vs Bounds for an explanation why.) This problem can be solved by creating a custom view. This answer shows how to do it for a UITextView, but it is the same basic concept for a label or button. (Note that you will have to remove the CGAffineTransformScale line in that answer since you don't need to mirror the text.)

自动布局不能与旋转视图一起工作。(请参阅框架vs界限来解释为什么。)这个问题可以通过创建自定义视图来解决。这个答案显示了如何对UITextView执行此操作,但它与标签或按钮的基本概念相同。(请注意,由于不需要镜像文本,所以必须删除该答案中的CGAffineTransformScale行。)

Related

相关的

#2


6  

swift

Rotate 90 degrees clockwise:

顺时针旋转90度:

    var rotateLabel: UILabel = UILabel(frame: CGRectMake(100, 0, 28, 159))

    rotateLabel.textAlignment = .Right

    rotateLabel.text = "Hello!"

    self.view.addSubview(rotateLabel)

    rotateLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

    rotateLabel.frame = CGRectMake(100, 0, 28, 159)

Rotate 90 degrees counterclockwise:

逆时针旋转90度:

    var rotateLabel: UILabel = UILabel(frame: CGRectMake(100, 0, 28, 159))

    rotateLabel.textAlignment = .Right

    rotateLabel.text = "Hello!"

    self.view.addSubview(rotateLabel)

    rotateLabel.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))

    rotateLabel.frame = CGRectMake(100, 0, 28, 159)

#3


1  

If you do this a lot, you'll wan't to make an extension. Also this will allow you to rotate your view 0-360 degrees.

如果你经常这样做,你就不会再做延期了。这也将允许你旋转你的视图0-360度。

extension UIView {
    func rotate(degrees: CGFloat) {
        rotate(radians: CGFloat.pi * degrees / 180.0)
    }

    func rotate(radians: CGFloat) {
        self.transform = CGAffineTransform(rotationAngle: radians)
    }
}

Then you can simply call rotate on your views

然后您可以简单地调用视图上的rotate

myView.rotate(degrees: 90)

#4


0  

Adapted the answer from Ananthu R Krishnan to fit into Swift 3.1.

改编了Ananthu R Krishnan的答案以适应Swift 3.1。

Rotate 90 degrees counterclockwise:

逆时针旋转90度:

let rotateLabel: UILabel = UILabel(frame: CGRect(x:15, y:66, width:28, height:159))
rotateLabel.textAlignment = .right
rotateLabel.text = "TEXT"
self.view.addSubview(rotateLabel)
rotateLabel.transform = CGAffineTransform(rotationAngle: CGFloat(-(Double.pi / 2.0)))
rotateLabel.frame = CGRect(x:15, y:66, width:28, height:159)