Rotate a UIlabel to make it vertical

Safari picture Safari · Aug 26, 2014 · Viewed 15.8k times · Source

I have a UILabel. I need to rotate it programmatically.

I have my horizontal UILabel, for example with frame: x:0, y:0, w: 200, h:80.

Now I would like to rotate the label to make it vertical: I try this code:

 [self setTransform:CGAffineTransformMakeRotation(M_PI_2 / 2)];

I can see the contained text rotated. But I would like to rotate the whole frame: With my code, the UILabel continues to have the same frame.

Answer

Ty Lertwichaiworawit picture Ty Lertwichaiworawit · Aug 26, 2014

Try this working code:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 80)];
//set background color to see if the frame is rotated
[label setBackgroundColor:[UIColor redColor]];
[label setText:@"Text Here"];
label.transform=CGAffineTransformMakeRotation( ( 90 * M_PI ) / 180 );
[self.view addSubview:label];

Hope it helps