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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:01:27+00:00 2026-06-17T17:01:27+00:00

I’ve searched around on how to perform this but I can’t find any answer.

  • 0

I’ve searched around on how to perform this but I can’t find any answer.
I’d like to know if my NSTextfield is already truncating the text (… at the end) without having to check the length of its stringValue. I need to do this to know if I should set a tooltip on my NSTextfield (acting like a simple label).

The reason I don’t want to check the length of my textfield’s stringValue it’s because there are some characters that occupy more space than others, so that’s not very accurate
Thanks!

  • 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-17T17:01:28+00:00Added an answer on June 17, 2026 at 5:01 pm

    Your best bet might be to use an NSTextView instead of a NSTextField. If you use an NSTextView you can get the NSTextContainer of the NSTextView using the textContainer property. The container can tell you the containerSize (the space the text is drawn in).

    NSString objects respond to the method sizeWithAttributes:. You can use the resulting NSSize struct to grab the width of the text if drawn with the given attributes. See the “Constants” section of the NSAttributedString Application Kit Additions Reference to figure out what attributes are relevant.

    If the containerSize width is less than the sizeWithAttributes: width then the text will be truncated.

    EDIT: Apologies, this is only true with no lineFragmentPadding, but the default lineFragmentPadding is non-zero. Either subtract the textContainer.lineFragmentPadding from containerSize.width or use the NSTextContainer setLineFragmentPadding: method to set it to 0.

    I suppose you could also make some assumptions about the text area relative to the size of the NSTextField and use the sizeWithAttributes: NSString method in conjunction with that, but that is not as clean.

    EDIT 2: I realized I did not address the OP’s interest in truncating the text using ellipses. The example code below uses truncation in the NSTextView. I also thought I might as well throw in some code that makes the NSTextView a little more similar in appearance to the NSTextField by putting it inside of a NSBox. Adding a check for size to determine if a tooltip should be displayed would be a simple addition to the code below using the information already mentioned above.

    NSString* string = @"Hello World.";
    
    // get the size the text will take up using the default font
    NSSize textBounds = [string sizeWithAttributes:@{}];
    
    // Create a border view that looks more or less like the border used around
    // text fields
    NSBox* borderView = [[NSBox alloc] initWithFrame:NSMakeRect(10, 10, 60, textBounds.height+4)];
    [borderView setBoxType:NSBoxCustom];
    [borderView setBorderType:NSBezelBorder];
    [borderView setContentViewMargins:NSMakeSize(0, 0)];
    [borderView setFillColor:[NSColor whiteColor]];
    
    // Create the text view
    NSTextView* textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 60, textBounds.height)];
    [textView setTextContainerInset:NSMakeSize(2, 0)];
    [textView.textContainer setLineFragmentPadding:0];
    [textView setEditable:YES];
    
    // Set the default paragraph style so the text is truncated rather than
    // wrapped
    NSMutableParagraphStyle* parStyle = [[NSMutableParagraphStyle alloc] init];
    [parStyle setLineBreakMode:NSLineBreakByTruncatingTail];
    
    // Do not let text get squashed to fit
    [parStyle setTighteningFactorForTruncation:0.0];
    
    [textView setDefaultParagraphStyle:parStyle];
    [parStyle release];
    
    // Set text
    [textView setString:string];
    
    // add NSTextView to border view
    [borderView addSubview:textView];
    [textView release];
    
    // add NSBox to view you want text view displayed in
    [self addSubview:borderView];
    [borderView release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

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.