VansFannel
VansFannel

Reputation: 45961

CGAffineTransformMakeRotation moves UILabel

I'm developing an iOS 5.0+ app with latest SDK.

I have a custom UIView (with a custom xib) and with four UILabel, and I want to rotate them 90 degrees with this code:

- (void)layoutSubviews
{
    [self rotateToLandscape];
}

- (void)rotateToLandscape
{
    NSLog(@"Label 1: %@", NSStringFromCGRect(self.label1.frame));
    NSLog(@"Label 2: %@", NSStringFromCGRect(self.label2.frame));
    NSLog(@"Label 3: %@", NSStringFromCGRect(self.label3.frame));
    NSLog(@"Label 4: %@", NSStringFromCGRect(self.label4.frame));

    self.label1.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 1: %@", NSStringFromCGRect(self.label1.frame));
    self.label2.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 2: %@", NSStringFromCGRect(self.label2.frame));
    self.label3.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 3: %@", NSStringFromCGRect(self.label3.frame));
    self.label4.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 4: %@", NSStringFromCGRect(self.label4.frame));
}

But, I don't why, all occupying the same position.

This is the log that I get:

Label 1: {{15, 0}, {71, 21}}
Label 2: {{15, 29}, {71, 21}}
Label 3: {{15, 58}, {71, 21}}
Label 4: {{15, 78}, {71, 21}}
Label 1: {{40, -25}, {21, 71}}
Label 2: {{40, 4}, {21, 71}}
Label 3: {{40, 33}, {21, 71}}
Label 4: {{40, 53}, {21, 71}}

I read that I have to rotate the UILabel from its center (or from its upper left corner), but I don't know how to do it.

This is what I get:

enter image description here

And, If I show the center to log:

- (void)rotateToLandscape
{
    NSLog(@"Label 1: %@", NSStringFromCGPoint(self.label1.center));
    NSLog(@"Label 2: %@", NSStringFromCGPoint(self.label2.center));
    NSLog(@"Label 3: %@", NSStringFromCGPoint(self.label3.center));
    NSLog(@"Label 4: %@", NSStringFromCGPoint(self.label4.center));

    self.label1.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 1: %@", NSStringFromCGPoint(self.label1.center));
    self.label2.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 2: %@", NSStringFromCGPoint(self.label2.center));
    self.label3.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 3: %@", NSStringFromCGPoint(self.label3.center));
    self.label4.transform = CGAffineTransformMakeRotation(M_PI/2);
    NSLog(@"Label 4: %@", NSStringFromCGPoint(self.label4.center));
}

I get this log:

Label 1: {50.5, 10.5}
Label 2: {50.5, 39.5}
Label 3: {50.5, 68.5}
Label 4: {50.5, 88.5}
Label 1: {50.5, 10.5}
Label 2: {50.5, 39.5}
Label 3: {50.5, 68.5}
Label 4: {50.5, 88.5}

Without rotation:

enter image description here

These labels are inside on an UIView and I think I have to rotate that parent view, instead of rotate each UILabel.

Any advice? Do I have to move each UILabel after rotation?

Upvotes: 1

Views: 1205

Answers (2)

VansFannel
VansFannel

Reputation: 45961

My solution:

Instead of rotate each UILabel I have to rotate parent UIView:

- (void)rotateToLandscape
{
    self.transform = CGAffineTransformMakeRotation(M_PI/2);
}

Upvotes: 1

David Rönnqvist
David Rönnqvist

Reputation: 56635

You should try and look at the documentation for the frame property.

Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

This means that the frame values you are printing after applying the rotation transform could be anything at all. Don't rely on that value. You could look at the center property if you want to know the position of the label.

By default all views are rotated around the center. You change it using the anchor point.

Upvotes: 3

Related Questions