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 6385511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:54:31+00:00 2026-05-25T02:54:31+00:00

I want my text to fit within a specific rect, so I need something

  • 0

I want my text to fit within a specific rect, so I need something to determine a font size. Questions have already tackled this to an extent, but they do a search, which seems horribly inefficient, especially if you want to be able to calculate during a live dragging resize. The following example could be improved to binary search and by constraining to the height, but it is still a search. Instead of searching, how can I calculate a font size to fit a rect?

#define kMaxFontSize    10000

- (CGFloat)fontSizeForAreaSize:(NSSize)areaSize withString:(NSString *)stringToSize usingFont:(NSString *)fontName;
{
    NSFont * displayFont = nil;
    NSSize stringSize = NSZeroSize;
    NSMutableDictionary * fontAttributes = [[NSMutableDictionary alloc] init];

    if (areaSize.width == 0.0 && areaSize.height == 0.0)
        return 0.0;

    NSUInteger fontLoop = 0;
    for (fontLoop = 1; fontLoop <= kMaxFontSize; fontLoop++) {
        displayFont = [[NSFontManager sharedFontManager] convertWeight:YES ofFont:[NSFont fontWithName:fontName size:fontLoop]];
        [fontAttributes setObject:displayFont forKey:NSFontAttributeName];
        stringSize = [stringToSize sizeWithAttributes:fontAttributes];

        if (stringSize.width > areaSize.width)
            break;
        if (stringSize.height > areaSize.height)
            break;
    }

    [fontAttributes release], fontAttributes = nil;

    return (CGFloat)fontLoop - 1.0;
}
  • 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-25T02:54:32+00:00Added an answer on May 25, 2026 at 2:54 am

    Pick any font size and measure the text at that size. Divide each of its dimensions (width and height) by the same dimension of your target rectangle, then divide the font size by the larger factor.

    Note that the text will measure on one line, since there is no maximum width for it to wrap to. For a long line/string, this may result in an unusefully-small font size. For a text field, you should simply enforce a minimum size (such as the small system font size), and set the field’s truncation behavior. If you intend to wrap the text, you’ll need to measure it with something that takes a bounding rectangle or size.

    Code by asker roughly based on this idea:

    -(float)scaleToAspectFit:(CGSize)source into:(CGSize)into padding:(float)padding
    {
        return MIN((into.width-padding) / source.width, (into.height-padding) / source.height);
    }
    
    -(NSFont*)fontSizedForAreaSize:(NSSize)size withString:(NSString*)string usingFont:(NSFont*)font;
    {
        NSFont* sampleFont = [NSFont fontWithDescriptor:font.fontDescriptor size:12.];//use standard size to prevent error accrual
        CGSize sampleSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:sampleFont, NSFontAttributeName, nil]];
        float scale = [self scaleToAspectFit:sampleSize into:size padding:10];
        return [NSFont fontWithDescriptor:font.fontDescriptor size:scale * sampleFont.pointSize];
    }
    
    -(void)windowDidResize:(NSNotification*)notification
    {
        text.font = [self fontSizedForAreaSize:text.frame.size withString:text.stringValue usingFont:text.font];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long text that would not fit within the div it occupies.
I have a UIButton that I want to resize to fit whatever text is
i'm using delphi,i have a text and i want to fit it into a
I have this div: <div class=inauguration-image> I do not want this text to display,
I want to capture text in an attribute within an XML tag. That is
I want to create a text box with a fixed width that will fit
hello i have a window(wpf) with labels and text boxes, i want him to
I want to, using JavaScript, chop the given text to fit the object in
I want to have the text inside of a div to basically be the
I want to make working button and fit it in this code i have

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.