Search code examples
objective-cmacoscocoatruncatenstextfield

How to check if NSTextfield is already truncating the text (... at the end)


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!


Solution

  • 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];