UILabel Wrapping

Shaun, Feb 13

The default behavior of UILabel is not to wrap, and unless you set the number of lines you want it to have, it doesn't.

You can use a UITextView to wrap, but it doesn't have some of the properties that UILabel has, specifically shadows, and there's too much unncessary overhead there since it uses a scroll view.

I took matters into my own hands and wrote a "UILabelWrap" class. It'll wrap to as many lines as it can fit in the frame you set, and after that, it'll truncate the end.

Here's the code:



Update March 10, 2009: I've updated the code above with one important bug fix and a minor compatibility fix:

  • Major: UILabelWrap now redraws when the frame size is changed, instead of stretching the text that was there. This will fix the issues when you rotate orientations.
  • Compatibility: Renamed highlightedColor to highlightedTextColor to maintain compatibility as a drop in replacement for UILabel
blog comments powered by Disqus