Understanding fonts and UIFont

For the first time in years, I had to dig inside one of the fundamental things we use everyday and everywhere: fonts.

Do you know how a fond is built? Applied to iOS, do you know how a UILabel will render a font?

First of all, a font is basically a container for vectors representing symbols from different categories such as letters, numbers, punctuation and other characters you can usually type with your keyboard. The more existing symbol a font contains, the more complete it is.

Within a font editor (Glyphs, Fontlab or whatever software allowing you to create and / or edit fonts) and for each symbol, the font “creator” draws vectorial paths according to some kind of grid. In iOS, UIFont’s grid looks like:

UIFont explained
UIFont as explained in Apple Developer documentation

The baseline if the base of all symbols. Then you have to distinguish the cases between minuscule letters with the x-height (X-height in the previous figure) since the minus “x” is supposed to be a perfect lower character; and the capital letters having their theoretical height represented in cap height.

Space on top of the baseline to the higher symbol Y offset is called the ascent or ascender. Space under the baseline and used for letter legs is called the descent or descender. There is more space under descent, it represents a gap to add between lines.

Adding the ascent to the descent and the line gap results in the line height which is basically the height of a single line of text.

That explains a lot! Did you ever notice that a label vertically centered in a view is not perfectly in the middle of the view?

It looks good...
It looks good…
... but if you zoom
… but if you zoom
...and it's even more visible if you add a background color to the label
…and it’s even more visible if you add a background color to the label

This is not a bug, just the way a font is rendered in a label. The extra space here is just the combination of both ascender and descender, and this space could be use (even if it is not in this case) to display accents, cedillas or other things that will be drew outside of the cap height bounding box.

Now, how can we respect a graphical chart dictating exact point size spacing between labels starting from the exact bottom of a label and ending to the exact top of another one?

chart
yeah, that’s always easier to say for designers when they are in Photoshop or InDesign…

So now, I have to work on a solution, the basic idea behind it is to subclass the UILabel class to have it read the metrics of UIFont’s and know its exact height. Even if it’s not working perfectly for every font (especially for some custom fonts that seems to be badly created) at the moment, the result is promising!

Promising results using the fitting label solution
Promising results using the fitting label solution

 


One thought on “Understanding fonts and UIFont

Leave a Reply

Your email address will not be published. Required fields are marked *