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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:01:33+00:00 2026-05-20T23:01:33+00:00

How can I calculate the height of a string within a particular label (NSTextField),

  • 0

How can I calculate the height of a string within a particular label (NSTextField), for some given fixed width?

I Googled up various methods and tried this method from Apple. It works, except that the height becomes one line too short for longer strings. Thus, there appear to be some inaccuracies in the present methods.

I also had the same issue with some other code I Googled up.

Does someone here have any accurate code for finding text height (given a particular width)?

  • 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-20T23:01:34+00:00Added an answer on May 20, 2026 at 11:01 pm

    I solved the problem using the NS(Attributed)String+Geometrics category provided at http://www.sheepsystems.com/sourceCode/sourceStringGeometrics.html Within the header file of that category is a good explanation of how these things work. Specifically, they mention that one should use NSTextView instead of NSTextField because it’s virtually impossible to get an accurate height for the latter.

    Thus, I replaced my NSTextField with NSTextView. First, I made my NSTextView look like my NSTextField with this code:

    [textView setEditable:YES];
    [textView insertText:someString];
    [textView setFont:[NSFont fontWithName:@"Lucida Grande"
       size:11] range:NSMakeRange(0,[someString length])];
    [textView setEditable:NO];
    [textView setSelectable:NO];
    

    Then I got the height with this code:

    NSDictionary *attributes
      = [NSDictionary dictionaryWithObjectsAndKeys:
         [textView font],NSFontAttributeName,nil];
    NSAttributedString *attributedString
      = [[NSAttributedString alloc] initWithString:
         [textView string] attributes:attributes];
    CGFloat height = [attributedString heightForWidth:
                     [textView frame].size.width];
    

    The height does indeed turn out to be accurate for NSTextView.

    I may have omitted some minor details, but you get the picture. I hope this helps someone out there.

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

Sidebar

Related Questions

how can i calculate width and height of a string when i draw that
I am trying to calculate an aspect ratio for an image: Log.d(img.width:, String.valueOf(img.getIntrinsicWidth())); Log.d(img.height:,
Is there any PHP/GD function that can calculate this: Input: image width, image height
How can I calculate the width and height of a <div> element, so as
How can I calculate the taskbar height, so that my winapi app starts up
I draw some text to UIVIew in drawRect: . First I calculate text height
I need to calculate the width and the height of the selected/highlighted text using
string str=this image has the width of 200px and height of 100px.; int width,
How can I calculate the height of the PDF document when using iTextSharp? I'm
I want to calculate height of browser using jquery so that i can set

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.