7

I am setting a UILabels frame based on what is returned by UIFont sizeWithFont but for whatever reason when i use a custom font, the values that are returned include some padding as seen below.

When i use boldSystemFontOfSize the text is vertically aligned in the middle (which is what i want), but when i use fontWithName i end up with padding under the text. Any reason why sizeWithFont is adding in the padding?

enter image description here

Heres my code...

CGRect frameLabel = label.frame;
CGSize sizeLabel = [label.text sizeWithFont:label.font];
frameLabel.size.width = sizeLabel.width;
frameLabel.size.height = sizeLabel.height;
[label setBackgroundColor:[UIColor redColor]];

** Edit **

I can calculate the top and bottom padding using this code and adjust the labels frame.origin.y to vertically center my label where it needs to be

float topPadding = [label.font ascender] - [label.font capHeight];
float bottomPadding = [label.font lineHeight] - [label.font ascender];
Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
AlBeebe
  • 8,101
  • 3
  • 51
  • 65
  • Is it possible that the font i am using "gotham" includes this padding and I would need to edit the font? – AlBeebe Sep 13 '11 at 07:42
  • Does this answer your question? [Custom UIFont baseline shifted](https://stackoverflow.com/questions/9015317/custom-uifont-baseline-shifted) – Robert Jul 08 '21 at 09:02

1 Answers1

3

Font is the only possible cause of this padding, but if you only need one-line labels, don't waste your time editing the font, just reduce the label's height by those few pixels after setting a proper frame by doing something like this:

label.frame = CGRectInset(label.frame, 0, bottomPadding);

Also, instead of:

CGRect frameLabel = label.frame;
CGSize sizeLabel = [label.text sizeWithFont:label.font];
frameLabel.size.width = sizeLabel.width;
frameLabel.size.height = sizeLabel.height;

You can just call:

[label sizeToFit];
Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
  • 1
    You confirmed my suspicion. Out of curiosity do you have any idea what property of the font is causing this extra padding at the bottom? – AlBeebe Sep 13 '11 at 09:53
  • Just confirmed 100% that it is the actual font that is causing the problem like you said. I downloaded FontForge and changed every single setting of gotham.ttf to match what Helvetica.ttf has and i was able to get it to remove the extra padding. – AlBeebe Sep 13 '11 at 10:53
  • Hi AlBeebe. I'm having the same issue and trying to change some font properties using FontForge. Could you explain me what values did you change? Thanks a lot! – LocoMike Feb 17 '12 at 20:57
  • LocoMike, late reply (9 months later) I'm not sure exactly what setting I changed. Like I said I changed all the properties to match Helvetica and it worked. – AlBeebe Nov 11 '12 at 16:46
  • 1
    Proposed solution won't help as letters like "y" will be cut in this case. I don't think we can fix it completely just by adjusting frame. I test on Lato font. – pronebird Jul 26 '14 at 13:01