views:

164

answers:

1

I got a subclass of UIView in which I draw an NSString object using the drawInRect:withFont:lineBreakMode:alignment: method.

Unfortunately, when I change the orientation of the device, say from portrait into landscape mode, the text doesn't get redrawn correctly but gets scaled and distorted against my intentions.

How can I solve this problem?

+1  A: 

This is part of how Core Animation animates transitions. A snapshot of your view is taken, and then stretched/moved into the new location.

There are a number of ways you can tackle this. First of all you can try this:

self.contentMode = UIViewContentModeRedraw;

This might be all you need and will tell Core Animation to redraw your contents instead of using a snapshot. If you still have issues, you can try defining a "stretchable" region that is stretched instead of your text.

For example, if you know you have a vertical slice of your view where there is never any text, you can define the content stretch to be in that little section and any stretching only occurs there, hopefully keeping your text intact.

CGFloat stretchStartX = 25.0f;
CGFloat stretchEndX = 30.0f;

/* Content stretch values are from 0.0-1.0, as a percentage of the view bounds */
self.contentStretch = CGRectMake(stretchStartX / self.bounds.size.width,
                                 0.0f,
                                 stretchEndX / self.bounds.size.width,
                                 1.0f);

This causes the your view to be stretched only between x values 25 and 30.

Mike Weller