iOS Custom Font Issue
and How to Fix it !
When you are developing a mobile app for iOS on Xcode, it sometimes happens that your custom font have some inner margin. So, if you are using it in a UILabel
or in an UIButton
, your text can be shifted and not appear centered, like this:
You can change all the margins and paddings you want, this text will continue to fight against you… And for good reason, the issue does not come from UIKit
but from your custom font itself. Dont worry, it fixable!
Fix it!
To fix this little issue, you need to follow the next steps:
- Download and install Apple Font Tools (the version of your installed Xcode)
- In your favorite terminal, go to the folder where your reluctant font and us this command line:
ftxdumperfuser -t hhea -A dfont.otf
(works also with.ttf
fonts). It will generate you a file named font.hhea.otf (respectively .ttf) - Open this file and modify the following values:
ascender
add it thelineGap
value (ifascender
= 750 andlineGap
= 200 then setascender
to 950) then setlineGap
to 0. - Save your modification then regenerate your font with the command line
ftxdumperfuser -t hhea -A f font.otf
(respectively.ttf
)
And that’s it! Normally your font should be correctly displayed in your application interface! No more grunt on Xcode!
To end, a designer joke:
One font meets the other in Rome. He asks: “Hey, are you a Roman too?” “No,” says the other, “but I am an Italic!”
Thanks for reading and have a good day !