views:

35

answers:

1

I have a UIView in which I need to draw text in drawRect:

- (void)drawRect:(CGRect)rect {
    ...
    [@"some text" drawAtPoint:somePoint withFont:someFont];
    ...
}

Because the text requires special formatting, I cannot just use a UILabel.

It looks fine until I rotate the device. Then the size of my custom UIView changes, and the text becomes stretched in one direction and squished in the other.

When I replace my view with a UILabel, the text always looks great, even when the bounds of the view changes.

How can I get my view to exhibit the same behavior as UILabel?

Some things I have looked into, but have yet to have success with:

  • Set the view's layer's needsDisplayOnBoundsChange to YES.
  • Set the view's contentStretch to CGRectZero.
  • Call setNeedsDisplay in my view's layoutSubviews.

Maybe I'm not doing one of these things right. Has anyone else run into this?

+1  A: 

Try setting the contentMode property of your view to UIViewContentModeRedraw.

James Huddleston
Nice, I didn't know that was hidden in there with the other content modes. With contentMode = UIViewContentModeRedraw the text now appears correct at the conclusion of the rotation. However, the text does warp during the transition. Any way to avoid that?
Hilton Campbell
Not that I know of. I think the way the animation works is that it has your `drawRect` method draw the post-rotation view at the post-rotation size, and then it scales that view on-the-fly (without calling `drawRect` again) to create the animation. So, if the post-rotation size of your view is smaller than the pre-rotation size, it will essentially draw your view at that smaller size, but then scale it up (and probably pixelate it) as the starting point for drawing the animation. Of course, any layout changes will be animated also. Does that make sense with what you're seeing?
James Huddleston
Yes, that's exactly what I'm seeing. The thing is, UILabel doesn't do that. It just immediately sizes and positions the view at its post-rotation frame and draws the text accordingly. It doesn't get animated at all.
Hilton Campbell
Perhaps `UILabel` gets special treatment during animation. Sorry ... I don't know a workaround for custom drawing.
James Huddleston