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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:24:11+00:00 2026-06-07T23:24:11+00:00

If I try to find out the width in pixels for a string CGSize

  • 0

If I try to find out the width in pixels for a string

CGSize sz = [@"Test text to hilight without new line for testing" sizeWithFont:CGTextLabel.font];

NSLog(NSStringFromCGSize(sz));

output is : CoregraphicsDrawing[3306:f803] {339, 21}

if i try separate the string by space @” “ and loop it to add width for each word + width for space after each word, total width is different

CoregraphicsDrawing[3306:f803] 351.000000

please check this code where I am calculating width word by word:

str = @"Test text to hilight without new line for testing";


    self.words = [NSMutableArray arrayWithArray:[str componentsSeparatedByString:@" "]];

    CGFloat pos = 0;
    [self.rects addObject:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
    for(int i = 0; i < [self.words count]; i++)
    {
        NSString *w = [self.words objectAtIndex:i];
        NSLog(w);
        CGSize sz = [w sizeWithFont:CGTextLabel.font];
        pos += sz.width;
        pos += [@" " sizeWithFont:CGTextLabel.font].width;

        if(i != [self.words count]-1)
        {

            [self.rects addObject:[NSValue valueWithCGPoint:CGPointMake(pos, 0)]];
        }
        else {
            NSLog(@"%f",pos); //here actual calculated width is printed. 
        }

    }

If anyone could suggest a solution, I would be really thankful.

  • 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-07T23:24:13+00:00Added an answer on June 7, 2026 at 11:24 pm

    After doing some testing, it looks like calling sizeWithFont: with just a space, adds extra room and does not treat it the same as a space between characters. This is what was happening when I tried to use systemFont.

    So everytime you use [@" " sizeWithFont:CGTextLabel.font].width; you are not going to get the size of a space between characters, but most likely some extra bits on the ends as well. I noticed this using the following:

    CGSize size1 = [@"Hello There" sizeWithFont:[UIFont systemFontOfSize:12]];
    CGSize size2 = [@" " sizeWithFont:[UIFont systemFontOfSize:12]];
    CGSize size3 = [@"Hello" sizeWithFont:[UIFont systemFontOfSize:12]];
    CGSize size4 = [@"There" sizeWithFont:[UIFont systemFontOfSize:12]];
    
    CGSize size5 = [@"Hello There Hello There" sizeWithFont:[UIFont systemFontOfSize:12]];
    
    NSLog(NSStringFromCGSize(size1));
    NSLog(NSStringFromCGSize(size2));
    NSLog(NSStringFromCGSize(size3));
    NSLog(NSStringFromCGSize(size4));
    NSLog(NSStringFromCGSize(size5));
    

    I got this returned from the console:

    2012-07-17 10:57:40.513 TesterProject[62378:f803] {63, 15}
    2012-07-17 10:57:40.514 TesterProject[62378:f803] {4, 15}
    2012-07-17 10:57:40.514 TesterProject[62378:f803] {28, 15}
    2012-07-17 10:57:40.514 TesterProject[62378:f803] {32, 15}
    2012-07-17 10:57:40.514 TesterProject[62378:f803] {128, 15}
    

    So the space alone is returning 4 “pixels” of width. “Hello” is returning 28, “There” is returning 32. So together you would tink it is 64, but I got 63. So the space is taking up 3 there instead of 4. Likewise, when I did “Hello There Hello There” you would think I would get 63*2 + 4 for the space = 130. Instead I got 63*2 + 2. So the space in this case is really only taking up 2-3 “pixels” most of the time but if I call sizeWithFont: for just a @" " I get 4.

    Fonts do not always create the exact same amount of gap for every space, and that is probably why you are having your issue. Through my testing I got the space as adding 2, 3, or even 4 “pixels” to different combinations of words. I think this call should only be used if you have the whole phrase, it is too difficult to try and add it all together. Is there another method to create the functionality you are looking for?

    Edit: I also found that just taking two words and putting them together will give you differing results from taking them individually:

    CGSize size = [@"HelloThere" sizeWithFont:[UIFont systemFontOfSize:12]];
    

    returned 59, when “Hello” returned 28 and “There” returned 32.

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

Sidebar

Related Questions

When I try to find out the total count of delicious bookmarks, Delicious returns
just to put it out there -I really did search and try to find
I keep getting this error when I try to call Find() public void findTxt(string
Guys, sorry for the basic question. I try to find out but I still
I need to find a way to work out the length and the width
I am working on a CMS template and try to find out if this
I really try to find on google how can we convert .doc and .txt
I have used DLL Export viewer to try and find the functions that are
I have been through every option to try to find a way to get
I have an Java AST and I try to find a variable inside it

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.