Embedded fonts in iOS: why are they not centered c

2019-02-06 09:53发布

问题:

I've embedded a couple fonts in my iOS app and am finding that they're not centered correctly in labels and buttons, they're too high on the y axis, is there any solution for this? At first I thought it was just a cheap font file I was using but then I tried it (just to see) with an Adobe font and it still has the issue.

回答1:

The (a?) solution a friend of mine pointed out is to use the contentEdgeInsets property of the button, I set it to:

myButton.contentEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 0);

To bump the text down 10 pixels and now the text is centered. Woot.



回答2:

I've too struggled with this.

Depending on the situation, I'm using different solutions:

  • using insets when the element in question supports it
  • replacing the label in question with a view, which holds another label as its subview. I then move that new label within it's superview until it is centered. This works with titlebars and the like.

What I have not tried yet is subclassing UILabel (or even better: writing a category on it) to include a fix.

Maybe I'll write a bug report as well.

I'll keep you posted on that!



回答3:

You should notice that UIControl classes have a property contentVerticalAlignment (as well as contentHorizontalAlignment).



回答4:

The centering problem only occurs in UILabels. UITextViews are working properly. UIButton has a UILabel inside, and so, has the same problem.

To fix it, you can correct your font position with a tool like Font Creator. In my case we had to correct it with -200. This way you can use the UILabel as you normally do, no subclassing or extra categories.

Font Creator download link: http://www.high-logic.com/fontcreator.html