2

I just need help with interface builder. I am trying to rotate only a label for my apps but I can't find the rotate function any where..

Can anyone help me to rotate a label, do I need code in xcode to rotate it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
shy
  • 107
  • 1
  • 5
  • 14

2 Answers2

8

In Swift 4, add the following code to your View Controller:

@IBDesignable
class DesignableLabel: UILabel {
}

extension UIView {
    @IBInspectable
    var rotation: Int {
        get {
            return 0
        } set {
            let radians = ((CGFloat.pi) * CGFloat(newValue) / CGFloat(180.0))
            self.transform = CGAffineTransform(rotationAngle: radians)
        }
    }
}

Then in interface builder, change your label class type in the Identity Inspector to "DesignableLabel". Your label should then be rotatable in interface builder.

Diskprotek
  • 601
  • 2
  • 7
  • 13
7

I don't think you can rotate it in IB. You need to appy a transform to the view to get it to rotate.

view.transform = CGAffineTransformMakeRotation(3.14/2);
David Neiss
  • 8,161
  • 2
  • 20
  • 21
  • how do i do that... and does it rotate just label or the whole interface.. thanks – shy Apr 25 '11 at 04:31
  • You can apply the transform to either a container view or a contained view. If container, then (I believe) should rotate all subviews within. – David Neiss Apr 25 '11 at 04:57
  • 4
    M_PI is defined in , so... CGAffineTransformMakeRotation(M_PI/2); – jbbenni Nov 06 '12 at 13:47