1

I'm trying to rotate a label on my view 90 degrees. I've tried the two following ways to do it and the label just disappears from the screen. I triple checked that the properties are properly attached. Any thoughts?

attempt one:

// rotating labels 90 degrees
self.labelCloseScroll.transform = CGAffineTransformMakeRotation (3.14/2);

attempt two:

CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
rotate = CGAffineTransformScale(rotate, 1, 1);
[self.labelCloseScroll setTransform:rotate];
Grymjack
  • 529
  • 1
  • 10
  • 21
  • Its working .. rotating 180 degree. Do you want it to disappear after completing rotations? – SachinVsSachin Jan 12 '13 at 08:33
  • 1
    3.14/2 rotates your label 90 degrees not 180 and transformation rotates the view around its center. So size of your label is important. Did you put NSLog to see new position of your label – user1111278 Jan 12 '13 at 08:33
  • In cases like this, try rotating the label by a small amount. Then you might see that the label is rotating around a strange centre point for some reason (which can be a cause of it disappearing when rotated by e.g. 180 degrees). – occulus Jan 12 '13 at 08:37
  • Ok I reduced the rotation amount and I can see the label on the screen `self.labelCloseScroll.transform = CGAffineTransformMakeRotation (3.14/10);` rotated a little but distorted. Making the number smaller by 1 each time rotates the text a little further and it appears to get more and more squashed (rotating on a strange axis?). By the time I get to `(3.14/5)` the label has disappeared. – Grymjack Jan 12 '13 at 08:59
  • @sachin No I don't want it to disappear. Actually all I want is a label with sideways text. Is there another way to do it? – Grymjack Jan 12 '13 at 09:18
  • @occulus and @user1111278 I can't seem to pull the current position. I tried to get the x,y,width, and height using `labelCloseScroll.frame.origin.x` prior to rotation and got all zeros. Is there another way to do this? – Grymjack Jan 12 '13 at 09:22
  • @Grymjack : Just Check my answer, If your code is similar then it will surely work. – Bhavin Jan 12 '13 at 09:50
  • 1
    Minor note, but you should use `M_PI` as a more accurate representation of pi. – Roy Jan 12 '13 at 11:25
  • @Louis thanks that turned out to be the issue, but I don't understand why...... – Grymjack Jan 12 '13 at 15:47

2 Answers2

1

I am not 100% sure if it works or not but why are you not using M_PI_2. It's just simple thought that you are assuming Value of Pi to be 3.14 but the exact value is 3.14159...

I did it like this and it worked fine :

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 70)];
lbl.text = @"New";
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.highlightedTextColor = [UIColor blackColor];
lbl.font = [UIFont systemFontOfSize:12];
lbl.transform = CGAffineTransformMakeRotation(M_PI_2);
[self.view addSubview:lbl];

You can also check Answers from these Questions :

How to Render a rotated UIlabel

Rotating UILabel at its center

Hope it will be helpful for you.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • That worked?!? `labelCloseScroll.transform = CGAffineTransformMakeRotation(M_PI_2);` What does (M_PI_2) do that (3.14/2) doesn't? M_PI_2 = 1.57079632679489661923132169163975144 or pi/2. My way was less precise, but I don't understand the reaction? – Grymjack Jan 12 '13 at 15:46
  • As a side note when I went M_PI_4, the text disappeared again. I can use M_PI_2, but I would still like to know why it s doing that.... – Grymjack Jan 12 '13 at 16:08
  • side note 2, when I use your straight code example rather than my synthesized label it works fine all the way around. think there might be some hidden constraint on it that I can't figure out. – Grymjack Jan 12 '13 at 16:22
  • using M_PI_2 instead of 3.14/2 should make no difference. most likely the problem is that the rotation is causing the label to rotate off screen. you probably will find your solution here: http://stackoverflow.com/questions/6160519/how-to-rotate-an-object-around-a-arbitrary-point – Joris Weimar May 11 '14 at 13:52
1

It may simply be that the view's bounds have become too small for the text. When the text can't be fully displayed in label view in iOS, it simply disappears, rather than remaining on show. Perhaps it's a deliberate Apple policy to prevent apps shipping with clipped text and forcing the dev to fix ;)

It sounds very much as though this is what is happening. You have said the text gets smaller as you rotate it, which indicates you have the shrink text to fit property set on the label view. This will shrink the text as the constraining view reduces in size. But the text will only shrink so much before it disappears.

If the label view itself would seem to be big enough, also be sure to check the bounds of each parent view the label is contained in, up through the view hierarchy.

TheBasicMind
  • 3,585
  • 1
  • 18
  • 20