ios 如何在 Swift 中为 UIButton 和 UILabel 旋转文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28717634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you rotate text for UIButton and UILabel in Swift?
提问by Suragch
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: How can you rotate text for UIButton and UILabel in Objective-C?
注意:在将其标记为重复之前,我有意将我的问题建模为以下问题的 Swift 版本:如何在 Objective-C 中为 UIButton 和 UILabel 旋转文本?
回答by Suragch
I am putting my answer in a similar format to this answer.
我的答案与此答案的格式类似。
Here is the original label:
这是原始标签:
Rotate 90 degrees clockwise:
顺时针旋转 90 度:
yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
Rotate 180 degrees:
旋转 180 度:
yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
Rotate 90 degrees counterclockwise:
逆时针旋转 90 度:
yourLabelName.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
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
The basic format is CGAffineTransform(rotationAngle: CGFloat)
where rotationAngle
is in radians, not degrees.
基本格式是CGAffineTransform(rotationAngle: CGFloat)
whererotationAngle
以弧度表示,而不是度数。
There are 2π radians in a full circle (360 degrees). Swift includes the useful constant CGFloat.pi
.
一个完整的圆(360 度)有 2π 弧度。Swift 包含有用的常量CGFloat.pi
。
CGFloat.pi
= π = 180 degreesCGFloat.pi / 2
= π/2 = 90 degrees
CGFloat.pi
= π = 180 度CGFloat.pi / 2
= π/2 = 90 度
Auto Layout:
自动布局:
Auto layout does not work with rotated views. (See Frame vs Boundsfor an explanation why.) This problem can be solved by creating a custom view. This answershows 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.)
自动布局不适用于旋转视图。(有关原因的解释,请参阅Frame vs Bounds。)这个问题可以通过创建自定义视图来解决。此答案显示了如何为 a 执行此操作UITextView
,但它与标签或按钮的基本概念相同。(请注意,您必须删除该CGAffineTransformScale
答案中的行,因为您不需要镜像文本。)
Related
有关的
回答by drees
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
然后你可以简单地调用你的视图旋转
myView.rotate(degrees: 90)
回答by Ananthu R Krishnan
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)
回答by DeepBlue
I would suggest using extension with options of degrees and counter/clockwise
我建议使用带有度数和逆时针选项的扩展
func rotate(degrees: Int , clockwise: Bool)
{
let x = 180 / degrees
if (clockwise)
{
self.transform = CGAffineTransform(rotationAngle: CGFloat.pi / CGFloat(x))
}
else
{
self.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / CGFloat(x))
}
}
I use it as extension UILabel {}
but that is obviously up to you
我用它,extension UILabel {}
但这显然取决于你
回答by boehlefeld
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)