UIButton custom font vertical alignment

IosObjective CFontsVertical Alignment

Ios Problem Overview


I've got a UIButton which uses a custom font, which is set when my view loads:

- (void)viewDidLoad
{
    [super viewDidLoad];    
    self.searchButton.titleLabel.font = [UIFont fontWithName: @"FONTNAME" size: 15.0 ];
}

The problem I've got is that the font is appearing to float up of the center line. If I comment out this line, the default font appears vertically centered fine. But changing to the custom font breaks the vertical alignment.

I'm getting the same issue on a Table Cell with a custom font too.

Do I need to tell the view somewhere that the custom font is not as tall as other fonts?

EDIT: I've just realized that the font I'm using is a Windows TrueType Font. I can use it fine in TextEdit on the Mac, only a problem with the alignment in my App

Button text not vertically centered

Ios Solutions


Solution 1 - Ios

A similar problem was discussed at https://stackoverflow.com/questions/5414730/custom-installed-font-not-displayed-correctly-in-uilabel. There was no solution given.

Here's the solution that worked for my custom font which had the same issue in UILabel, UIButton and such. The problem with the font turned out to be the fact that its ascender property was too small compared to the value of system fonts. Ascender is a vertical whitespace above font's characters. To fix your font you will have to download Apple Font Tool Suite command line utilities. Then take your font and do the following:

~$ ftxdumperfuser -t hhea -A d Bold.ttf

This will create Bold.hhea.xml. Open it with a text editor and increase the value of ascender attribute. You will have to experiment a little to find out the exact value that works best for you. In my case I changed it from 750 to 1200. Then run the utility again with the following command line to merge your changes back into the ttf file:

~$ ftxdumperfuser -t hhea -A f Bold.ttf

Then just use the resulting ttf font in your app.

OS X El Capitan

The Apple Font Tool Suite Installer doesn't work anymore on OSX El Capitan because of SIP because it tries to install the binary files into a protected directory. You have to manually extract ftxdumperfuser. First copy the pkg from the dmg to a local directory afterwards unpack the OS X Font Tools.pkg with

~$ xar -xf OS\ X\ Font\ Tools.pkg

Now navigate into the folder fontTools.pkg with

~$ cd fontTools.pkg/

Extract payload with

~$ cat Payload | gunzip -dc | cpio -i

Now the ftxdumperfuser binary is in your current folder. You could move it to /usr/local/bin/ so that you can use it in every folder inside of the terminal application with the following.

~$ mv ftxdumperfuser /usr/local/bin/

Solution 2 - Ios

I solved the problem adjusting the top content (not the title!) inset.

For example: button.contentEdgeInsets = UIEdgeInsetsMake(10.0, 0.0, 0.0, 0.0);

Good luck!

Solution 3 - Ios

Not sure if this will help as it may depend on your font, but it could be that your baseline is misaligned.

self.searchButton.titleLabel.baselineAdjustment = 
    UIBaselineAdjustmentAlignCenters;

Solution 4 - Ios

I think this is the best answer. no playing with ascender, numberOfHMetrics etc... just import-export by Glyphs application and Job done. Thanks to this answer: https://stackoverflow.com/a/16798036/1207684

Solution 5 - Ios

Late to the party, but as this issue hit me for the Nth time, I thought I'd post the simplest solution I've found: using Python FontTools.

  1. Install Python 3 if it's not available on your system.

  2. Install FontTools

pip3 install fonttools

FontTools include a TTX tool which enables conversion to and from XML.

  1. Convert your font to .ttx in the same folder

ttx myFontFile.otf

  1. Make the necessary edits to .ttx and delete the .otf file as this will be replaced in the next step.
  2. Convert the file back to .otf

ttx myFontFile.ttx


Motivation: The solution by kolyuchi is incomplete, and even with this extended installation flow, running ftxdumperfuser resulted in an error on 10.15.2 Catalina.

Solution 6 - Ios

You can try this out in Interface Builder. Here is a snapshot of how to do it -

enter image description here enter image description here

As you can see trying to do this in IB has its own benefits.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeteView Question on Stackoverflow
Solution 1 - IoskolyuchiyView Answer on Stackoverflow
Solution 2 - IosJordi CorominasView Answer on Stackoverflow
Solution 3 - IosMatsView Answer on Stackoverflow
Solution 4 - Iosdollar2048View Answer on Stackoverflow
Solution 5 - IosNiFiView Answer on Stackoverflow
Solution 6 - IosSrikar AppalarajuView Answer on Stackoverflow