How can you rotate text for UIButton and UILabel in Swift?

IosSwiftUibuttonRotationUilabel

Ios Problem Overview


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

Note: Before you mark this as a duplicate, I am intentionally modelling my question as a Swift version of this one: https://stackoverflow.com/questions/6326720/objective-c-how-can-you-rotate-text-for-uibutton-and-uilabel

Ios Solutions


Solution 1 - Ios

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

Here is the original label:

enter image description here

Rotate 90 degrees clockwise:

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

enter image description here

Rotate 180 degrees:

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

enter image description here

Rotate 90 degrees counterclockwise:

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

enter image description here

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.

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

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

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.)

Related

Solution 2 - Ios

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.

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)

Solution 3 - Ios

swift

Rotate 90 degrees clockwise:

    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:

    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)

Solution 4 - Ios

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

Solution 5 - Ios

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

Rotate 90 degrees counterclockwise:

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)



Solution 6 - Ios

If you use a custom font you maybe need:

rotateLabel.sizeToFit()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuragchView Question on Stackoverflow
Solution 1 - IosSuragchView Answer on Stackoverflow
Solution 2 - IosdsreesView Answer on Stackoverflow
Solution 3 - IosAnanthu R KrishnanView Answer on Stackoverflow
Solution 4 - IosDeepBlueView Answer on Stackoverflow
Solution 5 - IosboehlefeldView Answer on Stackoverflow
Solution 6 - IosJulio GómezView Answer on Stackoverflow