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

  • SEARCH
  • Home
  • 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 8637539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:30:03+00:00 2026-06-12T10:30:03+00:00

I have a UILabel in which the text is automatically shrunk to fit. Once

  • 0

I have a UILabel in which the text is automatically shrunk to fit. Once this has happened, I need to know the point size of the resulting font so that I can set that exact same size in another label elsewhere.

If I access label.font.pointSize I get back the original size, not the shrunk size. So I then tried using this NSString addition to calculate the font size:

- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

However, that doesn’t work for me either because if doesn’t take into account the label’s height constraint, only its width. (In my case the font needs shrinking further to fit in the frame’s height).

This seems to leave me with just one absolutely horrible option, and that’s to call the following function repeatedly until I’ve found the maximum height that will actually fit into the frame:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

Clearly that would be both very inefficient and nasty.

Does anyone have a better solution?

Tim

  • 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-06-12T10:30:05+00:00Added an answer on June 12, 2026 at 10:30 am

    Well, in the advent that no-one can provide me with a better option, I thought I’d share a category that I’ve written to solve the problem the hard way.

    However, if there’s a direct way to access the shrunk font size of a UILabel that’d obviously be ideal…

    Without further ado, this function will return an auto-shrunk font size that fits within the constrained size.

    Interface:

     @interface NSString (FontSize)
    
    - (CGSize)sizeWithFont:(UIFont *)font
               minFontSize:(CGFloat)minFontSize
            actualFontSize:(CGFloat *)actualFontSize
         constrainedToSize:(CGSize)maxSize
             lineBreakMode:(UILineBreakMode)lineBreakMode;
    
    @end
    

    Implementation:

    @implementation NSString (FontSize)
    
    - (CGSize)sizeWithFont:(UIFont *)font
               minFontSize:(CGFloat)minFontSize
            actualFontSize:(CGFloat *)actualFontSize
         constrainedToSize:(CGSize)maxSize
             lineBreakMode:(UILineBreakMode)lineBreakMode
    {
        CGFloat size;
        CGSize boundingBox;
    
        // First, get the OS to fit the text into the width
    
        boundingBox = [self sizeWithFont:font
                             minFontSize:minFontSize
                          actualFontSize:&size
                                forWidth:maxSize.width
                           lineBreakMode:lineBreakMode];
    
        // Return here if the min size has already been reached
    
        if (size <= minFontSize)
        {
            *actualFontSize = size;
            return boundingBox;
        }
    
        // Binary search biggest size that will fit within the height of the box    
    
        CGFloat upperBound = size;
        CGFloat lowerBound = minFontSize;
    
        UIFont *f;
        CGFloat fontSize;
        while (upperBound > lowerBound + 0.5) {
    
            fontSize = (upperBound + lowerBound) / 2;
    
            f = [UIFont fontWithName:font.fontName size:fontSize];
            boundingBox = [self sizeWithFont:f];
    
            if (boundingBox.height > maxSize.height)
                upperBound = fontSize - 0.5;
            else
                lowerBound = fontSize;
        }
    
        // Return the result
    
        *actualFontSize = fontSize;
        return boundingBox;
    }
    
    @end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UILabel which should have transparency (alpha) = .6 but Text of
I have a UILabel on which I perform a CGAffineTransformConcat to tilt the text
I have two UILabel in side of UICell , which contains dynamic text so
I have a subclassed UITableViewCell called Comment, which has a UILabel containing some relevant
I have a UILabel which has UILabel attached to it. I add NSString value
I have a UILabel which autoresizes along with its parent view. The label has
I have these Views on which I add UILabels as text, The Views can
I am creating an app which will have a question in a UILabel and
I have a UILabel with space for two lines of text. Sometimes, when the
I have a UILabel with right-aligned text that may vary from between three to

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.