Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate a UIlabel to make it vertical

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.

like image 735
Safari Avatar asked Aug 26 '14 10:08

Safari


2 Answers

If you prefer set the label position and size visually in you xib or storyboard do the following:

  1. Set the labels position and size in the interface builder like you want them to stay after the rotation.

  2. Rotate the label and set the frame again:

    -(void)rotateLabel:(UILabel*) label
    {
        CGRect orig = label.frame;
        label.transform=CGAffineTransformMakeRotation(M_PI * 3/2);//270º
        label.frame = orig;
    }
    
like image 52
Viker Avatar answered Sep 19 '22 17:09

Viker


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

like image 24
Ty Lertwichaiworawit Avatar answered Sep 17 '22 17:09

Ty Lertwichaiworawit