Archive for February 8th, 2010


Class: AutoScrollLabel

Here’s a handy little class … well, handy if you want your UILabel to be a scrolling marquee. Fairly straightforward to do that in any number of ways, but this particular one is mildly notable for leveraging native facilities:

… The scrolling is implemented using two UILabels contained in a UIScrollView. Each label contains an identical copy of the text and the labels are resized to be just large enough to hold the text without any truncation.

If the text in the UILabel is wider than the containing UIScrollView, then the two UILabels are laid out side-by-side, and the scroll view is animated to scroll from the beginning of one label to the beginning of the next label. The second label is there just to give the illusion of the text looping forever.

The class registers itself as the delegate for the scrolling animation block, so when the animation block ends, it waits the defined pause time, and then just performs another animated scroll, in which case the scroll view is reset back to the beginning of the first label and the process is repeated.

And that’s it! Pretty simple. The animation block feature of a UIView does all the hard work.

Yes, we do tend to overlook just how powerful an engine we have sitting there with UIViewAnimation, indeed. So here’s another reminder!

h/t: iPhoneSDK!