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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:45:55+00:00 2026-06-06T10:45:55+00:00

I have 4 lines UILabel with exact frame and font. I need to know

  • 0

I have 4 lines UILabel with exact frame and font.

I need to know if this string fits the label and what is the index of the last character which fits.

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

    The kernel of the answer is in Cupcake’s referenced posting. Anyway, you can use sizeWithFont:constrainedToSize:lineBreakMode: to figure out what the size of a frame would be with a particular font in a label of a given width given a specific word wrapping, e.g.

    CGSize size = [string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:UILineBreakModeWordWrap];
    

    Set sizeConstraint to be the same width of your label, but set the height to be larger. If the resulting size.height is larger than your UILabel, then your string is too long. Theoretically, you could remove the last character/word and try again and repeat until it fits.

    If you think the strings might be very long, you might want to go the other way, start with a short portion of the string and keep adding characters until it’s too large, and then you know the last character.

    Either way, this iterative calculation of the size can be pretty cpu intensive operation, so be careful.

    Update:

    Here is an algorithm that returns the length of NSString that can fit into the UILabel in question using the default font (but ignoring minimum font size):

    - (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label
    {
        UIFont *font           = label.font;
        UILineBreakMode mode   = label.lineBreakMode;
    
        CGFloat labelWidth     = label.frame.size.width;
        CGFloat labelHeight    = label.frame.size.height;
        CGSize  sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX);
    
        if ([string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight) 
        {
            NSString *adjustedString;
    
            for (NSUInteger i = 1; i < [string length]; i++) 
            {
                adjustedString = [string substringToIndex:i];
    
                if ([adjustedString sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight)
                    return i - 1;
            }
        }
    
        return [string length];
    }
    

    You could probably make this more efficient if you, for example, checked if word break mode, jumping to the next word separator and then calling sizeWithFont, but for small UILabels this might be sufficient. If you wanted to leverage word-wrap logic to minimize the number of times you call sizeWithFont, you might have something like:

    - (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label
    {
        UIFont *font           = label.font;
        UILineBreakMode mode   = label.lineBreakMode;
    
        CGFloat labelWidth     = label.frame.size.width;
        CGFloat labelHeight    = label.frame.size.height;
        CGSize  sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX);
    
        if ([string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight) 
        {
            NSUInteger index = 0;
            NSUInteger prev;
            NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    
            do 
            {
                prev = index;
                if (mode == UILineBreakModeCharacterWrap)
                    index++;
                else
                    index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
            }
            while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height <= labelHeight);
    
            return prev;
        }
    
        return [string length];
    }
    

    Perhaps the character set used here isn’t quite right (should you include hyphens, for example), but it’s probably pretty close and far more efficient than doing character by character, if you don’t need to do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamic multi line UILabel and need to know the end of
A UILabel's text is drawn in a CGRect(x,y,size,width). The label can have multiple lines
do I need separate UILabel in a custom UIView to have separate UILabel lines
I have a UILabel . I need to set the number of lines to
I have a UILabel with space for two lines of text. Sometimes, when the
I have lines in my js files like this console.log('FunctionName()'); The default Ajax Minifier
I have a UILabel with some text, say Hello World abcdefg The label can
I have a RoundedRectView with a UILabel that holds lines of text of varying
Is there a way to have multiple lines of text in UILabel like in
I have a UILabel with text: 照明タイマーをセットする that is being cut into two lines

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.