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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:26:11+00:00 2026-05-26T21:26:11+00:00

sizeWithFont:constrainedToSize:lineBreakMode: don’t seem to be returning me the correct width. After these codes are

  • 0

sizeWithFont:constrainedToSize:lineBreakMode: don’t seem to be returning me the correct width. After these codes are executed, I see that part of the string in the label is chopping off, which means I’ve to manually add a few pixels to the size. Am I missing something?

I’ve a UILabel:

theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, LABELWIDTH, LABELHEIGHT)];
theLabel.lineBreakMode = UILineBreakModeWordWrap;
theLabel.numberOfLines = 0;
[theLabel setFont:[UIFont fontWithName:@"MarkerFelt-Wide" size:16]];
theLabel.textColor = [UIColor blackColor];
theLabel.textAlignment =  UITextAlignmentLeft;
theLabel.backgroundColor = [UIColor clearColor];

I tried to programmatically modify the size of the label using the following:

CGSize maximumLabelSize = CGSizeMake(LABELWIDTH, 10000);

CGSize expectedLabelSize = [someString sizeWithFont:theLabel.font constrainedToSize:maximumLabelSize lineBreakMode:theLabel.lineBreakMode];

theLabel.text = someString;

CGRect newFrame = theLabel.frame;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = newFrame.size.width+50;
theLabel.frame = newFrame;
  • 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-26T21:26:12+00:00Added an answer on May 26, 2026 at 9:26 pm

    Ok, well, the first thing I’ll say is that there are some very useful ways to deal with frames that you currently aren’t employing. For example, your code,

    CGRect newFrame = theLabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    newFrame.size.width = newFrame.size.width+50;
    theLabel.frame = newFrame;
    

    Can be rewritten with functions from CGGeometry,

    CGFloat widthOffset = 50.0f;
    theLabel.frame = CGRectOffset(CGRectInset(theLabel.frame, widthOffset, 0.0f), widthOffset / 2.0f, 0.0f);
    

    However, if your code worked as it was intended, you would not need to do this at all. You can go two routes,

    [theLabel sizeToFit];
    

    Or, this should also work,

    theLabel.frame = CGRectMake(theLabel.frame.origin.x, theLabel.frame.origin.y, expectedLabelSize.width, expectedLabelSize.height);
    

    No where in your earlier code did you change the width of theLabel to match the expected width. Notice, you wrote newFrame.size.width = newFrame.size.width+50 and that should be newFrame.size.width = expectedLabelSize.width.

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

Sidebar

Related Questions

I'm attempting to use sizeWithFont as follows: [commentTextLabel.text sizeWithFont:commentTextLabel.font constrainedToSize:CGSizeMake(commentTextLabel.frame.size.width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; When I
I can't find the documentation for the NSString sizeWithFont:forWidth:lineBreakMode: method (see Kevin Ballard 's
I got this warning: The receiver of message 'sizeWithFont:constrainedToSize:lineBreakMode:' is nil and returns a
In my question How do I get -[NSString sizeWithFont:forWidth:lineBreakMode:] to work? , I learned
I'm using heightForRowAtIndexPath and sizeWithFont to calculate the height for a cell. After 3
So I am seemingly encountering some strange behavior when using NSString 's -sizeWithFont family
I have a long text string (including \n newline characters) that I feed into
I have a UITextView embedded inside a grouped table view cell that I can't
I need to find the on screen size (CGSize) of a string that will
I am facing an issue in getting the correct frame for two different strings.

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.