Rotate UIlabel to make it vertical

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

I have my horizontal UILabel, for example, with a 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 see that the contained text is rotated. But I would like to rotate the whole frame: with my code, UILabel continues to have the same frame.

+6
source share
3 answers

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 this helps

+21
source

If you prefer to visually visualize the position and size of the label in xib or storyboard, follow these steps:

  • Set the location and size of the labels in the interface builder as you want them to remain after rotation.

  • Turn 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; } 
+2
source

Your label is a square (w: 100, h: 100). So your shortcut has changed, but you do not see the change because the width is equal to the height.

0
source

Source: https://habr.com/ru/post/974360/


All Articles