Animating Font Size In Uilabels
This post originally appeared on Medium.
I’ve noticed a loading screen design pattern where a placeholder gets swept up to the title position.
Let’s say you want to do that with a UILabel. We access a label’s font size through its font, but Apple hasn’t made the label.font implicitly animatable so we can’t simply go like this:
However, we can enlarge it by hitting the transform:
Yikes. That looks pixellated when blown up because the small font rendering doesn’t contain enough detailed information to look good at a higher resolution.
A better strategy:
-
Create the larger sized label first.
-
Scale it down to mimic the smaller label.
-
Animate it back to its normal scale.
This way we get the benefit of a more detailed render.
We’ll use a different strategy to shrink it:
-
Scale down the transform of the larger label.
-
Swap in the smaller font at the end.
Not bad… but see the slight jitter at the end?
That happens because a scaled down label at the larger size doesn’t look exactly the same as the smaller label. Pretty close, but not pixel perfect.
Some fonts change all sorts of properties at different sizes to make them more readable. The character spacing might be different. The way the glyphs are drawn might be different. The font creator will do whatever it takes to help their readers.
Now, this isn’t that big of a deal. You could use the above animation and call it a day.
If you want extra credit though, throw in a cross-fade between the two labels. To see the code for this check out my GitHub project: https://github.com/regularberry/AnimatedFontSize
Sean works as an iOS developer at Livefront and has animated many fonts in his algebra app.