UIButton custom font vertical alignment
Asked Answered
I

7

122

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

Idiocrasy answered 23/9, 2011 at 22:34 Comment(1)
I am having the same issue with this font (.otf, not .ttf): fontsquirrel.com/fonts/bebas-neueNannie
C
325

A similar problem was discussed at 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/
Conversation answered 29/11, 2011 at 16:27 Comment(28)
+1 Great tip! Here's some info about Ascenders and Descenders typophile.com/node/13081Fordham
If Apple Font Tool Suite doesn't work for you, there is also a free tool called MensisConversation
This looks much better than my padding hack. I'll have to try it out.Idiocrasy
Worked equally well for OTF files.Microeconomics
This answer is awesome advice. I needed to adjust both the ascender and the lineGap to get things right.Bashemath
i had to decrease the ascender. but that't nice!Truncate
If you use a custom font modified with this solution, and you set text i.e. in a UITextView with multiple lines, the interline distance becomes way too big.Footlambert
worked like a charm .. thank you very much .. and i second @AccatyycHouri
I used Type font editor (cr8software.net/type.html) which has a free edition and also runs on Windows.Earhart
This is perfect! I was tired of setting edge insets and stuff like that, thank you!Schizophyceous
I've found that in iOS 7 beta 6, they've fixed the font alignment issue. So if you do this fix, it will be good in iOS 6, but broken in iOS 7.... 7 is still in beta though, so who knowsAspiration
@Aspiration I found the same problem too, though managed to find a nice balance between the two iOS version. By the way there is also a descender property which I found you had to update in conjunction with the ascender, otherwise weird things can happen.Dedans
@Dedans by nice balance... you mean they're both off, but only slightly?Aspiration
@Aspiration Lol yep that's exactly what I meant :(Dedans
@Aspiration did you find any solution allready - i'm having the same problem.Terrence
I haven't done it yet, but I'd include two font files for each font you have - one with this fix and one without. In code, do a check on the ios version number to decide which font to set. Not awesome, I know, but seems unlikely there's a better way.Aspiration
this does fix on iOS 6 but won't help on iOS 7. I've posted a separate question here: #19036551Summon
Thanks Man , thats works like charm even on ios7. In my case i have to manipulate the descender property as my text was being cut from below ..Ajay
when trying to save back the font I got some illegal charaecters in the XML. Apperently the " changes to an illegal one for every value I edited. To fix that I had to copy-paste the " char from value that I didn't edit (On Text Edit. The illegal one: , and the copied legal one: ").Count
Yes. Reducing the lineGap to zero (0) and leaving the ascender (at 1991) solved the problem for me with the free "Overpass" font from Fedora. Thank you all for guidance to a solution. Works on Both iOS6 and iOS7Intracellular
Trying this with a custom font I was provided.. it just doesn't work. Doesn't have any effect at all. OTF font btw.Balsam
I tried to install the Apple Font Suite on OS X El Capitan, however it can't be installed because it is not compatible anymore to the current OS X version. Did anyone find a way to get it running on El Capitan?Fatuous
Apple Font Tools cannot install in Macbook Pro El Capitan. See apple.stackexchange.com/questions/211138/… for workarounds.Bunt
I'm on El Capitan and could successfully use ftxdumperfuser thanks to this answer and the comment up there by @VadimYelagin . In my case I just edited the descender. Thanks everyone. no-spec.com/images/3Thumb_Badge_Sm.gifClassless
Can't download Apple Font Tool Suite anymore. {"responseId":"dd0a6a0a-b26c-407a-976f-d30d3f131439","resultCode":1003,"resultString":"request.uri.notfound","userString":"Invalid request, Service mapping to the requested URL is not available. ","creationTimestamp":"2016-07-15T08:20:44Z","userLocale":"en_US","requestUrl":"developer.apple.com:443/downloads/…}Syndactyl
Apple Font Tools can be easily installed for OS X Sierra without any workarounds (at least in my case). Glyphs application did not help me. I had issue with cutting off letters like "Â" in Portuguese using MyriadPro-Regular font. Solved with increasing of ascender value.Despicable
Font tools have been updated for Xcode 9, they install fine on High Sierra. Direct link: developer.apple.com/download/more/?=fontPivotal
The above instructions are incomplete; you can call ftxdumperfuser, but it will fail because it relies on FontToolbox.framework to be in /Library/Frameworks. You need to unpack and move files from fonttoolbox.pkg also. Complete instructions: apple.stackexchange.com/a/328214/254594Siphonophore
N
49

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!

Noachian answered 20/10, 2011 at 11:34 Comment(2)
This was a good quick solution for me which seemed simpler than trying to edit the font. myButton.titleLabel.font = [UIFont fontWithName:@"FontName" size:20.0]; myButton.contentEdgeInsets = UIEdgeInsetsMake(3.0, 0.0, 0.0, 0.0);Blau
Brilliant! Thank you! My problem was slightly different, I had simple + and - buttons using the default font and they were appearing lower than centered, I was able to use the solution here to raise the text slightly so it didn't look off.Extenuate
S
7

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;
Sarracenia answered 24/9, 2011 at 11:31 Comment(1)
Works great on a titleLabel for a UIButton when the font size is being adjusted with minimumScaleFactorSnowblind
F
6

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://mcmap.net/q/23546/-custom-installed-font-not-displayed-correctly-in-uilabel

Forsaken answered 21/3, 2014 at 18:12 Comment(2)
You have saved my day!Shelving
Mine too! Thanks sir.Hollywood
M
3

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.

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

    ttx myFontFile.otf

  4. Make the necessary edits to .ttx and delete the .otf file as this will be replaced in the next step.

  5. 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.
Mislay answered 10/2, 2020 at 13:23 Comment(0)
A
2

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.

Aureliaaurelian answered 24/9, 2011 at 11:22 Comment(3)
That setting is already in place, and in IB the font shows vertically, but shows as the default font. It is only when I run my app that my custom font face shows up, and it is not centered verticallyIdiocrasy
do you set your "custom font" in IB? is it supported in iOS fonts?Aureliaaurelian
The font is set in IB, and also by the code specified at the in my original post. It doesn't actually appear in the IB screen mockup, although it does in font select drop down in IB.Idiocrasy
S
-1

Switching from an otf to a ttf version of the font might be a possible fix for this issue, depending on the font.

Sexless answered 20/4, 2023 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.