How do I get a UIView which displays text to behave like a UILabel when bounds change?

Posted by Hilton Campbell on Stack Overflow See other posts from Stack Overflow or by Hilton Campbell
Published on 2010-10-16T16:26:43Z Indexed on 2011/01/03 13:53 UTC
Read the original article Hit count: 198

Filed under:
|
|

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 (in the parent view's layoutSubviews method), 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?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiview