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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:48:17+00:00 2026-05-14T14:48:17+00:00

A quicky: how can I get the size (width) of a NSString? I’m trying

  • 0

A “quicky”: how can I get the size (width) of a NSString?

I’m trying to see if the string width of a string to see if it is bigger than a given width of screen, case in which I have to “crop” it and append it with “…”, getting the usual behavior of a UILabel. string.length won’t do the trick since AAAAAAAA and iiiiii have the same length but different sizes (for example).

I’m kind of stuck.

Thanks a lot.

  • 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-14T14:48:17+00:00Added an answer on May 14, 2026 at 2:48 pm

    This is a different approach. Find out the minimum size of the text so that it won’t wrap to more than one line. If it wraps to over one line, you can find out using the height.

    You can use this code:

    CGSize maximumSize = CGSizeMake(300, 9999);
    NSString *myString = @"This is a long string which wraps";
    UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize myStringSize = [myString sizeWithFont:myFont 
                               constrainedToSize:maximumSize 
                                   lineBreakMode:self.myLabel.lineBreakMode];
    

    300 is the width of the screen with a little space for margins. You should substitute your own values for font and size, and for the lineBreakMode if you’re not using IB.

    Now myStringSize will contain a height which you can check against the height of something you know is only 1 line high (using the same font and size). If it’s bigger, you’ll need to cut the text. Note that you should add a … to the string before you check it again (adding the … might push it over the limit again).

    Put this code in a loop to cut the text, then check again for the correct height.

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

Sidebar

Related Questions

Logging can get complicated, quickly. Considering that you have some code, how do you
I'm attempting to use jqgrid. Unfortunately, I can't compute a resultset size because I'm
I like ReSharper, but it is a total memory hog. It can quickly swell
Just for review, can someone quickly explain what prevents this from working (on compile):
How can I write the following code quickly in emacs? \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B}
Can anyone recommend a tool for quickly posting test messages onto a JMS queue?
Can anyone tell me how to quickly copy Files from a mapped network drive?
Where can I find a free, very quick, and reliable implementation of FFT in
I'm writing a quick Rails app and was wondering how I can validate the
A quick Google search of this issue shows it's common, I just can't for

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.