Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:17:35+00:00 2026-05-25T15:17:35+00:00

I am setting a UILabels frame based on what is returned by UIFont sizeWithFont

  • 0

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];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:17:36+00:00Added an answer on May 25, 2026 at 3:17 pm

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem setting the font for UILabels and UITextViews with Interface Builder.
I have created a custom UIView that has some set of UILabels in it.
Setting up a Windows based web application on Amazon's cloud has definitely been a
Setting a Magento store for a winery. They well bottles of wine, but you
I have a custom table cell which contains a number of UILabels. At runtime,
I am creating a custom UITableViewCell with UILabel inside. When I am setting too
I have an array of UILabels inside the contentView of a custom UITableViewCell. The
I have an app where I am setting the font for a UILabel programmatically
I've looked at the following link: Setting UILabel - Font through code - generates
In WWDC 2011 video 309 on Storyboards. They demonstrate using a custom UITableViewCell that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.