Truncating the string value of an NSTextField, in a Panther compatible way.

  • Hey Everyone,

    I'm trying to determine the best way to enable an NSTextField to
    truncate its stringValue (using one of the NSLineBreakMode options)
    if that value is too long and won't entirely fit into the field's
    frame.  My text field has been sized in Interface Builder so that it
    will shrink as the window around it is made smaller.

    Before you hit reply: I realize this is very simple on 10.4 or later,
    with a message to a cell's setWraps:YES and setLineBreakMode:.  IB
    also allows both of these attributes to be set easily.

    I'm in need of a solution that will work on 10.3.9+, however.  The
    setLineBreakMode: method was added to the AppKit in Tiger <http://
    developer.apple.com/releasenotes/Cocoa/AppKit.html>.  The archives
    and other usual resources, for the most part, only turned up ideas
    about adding this behavior to text inside an NSTableView.

    Using an attributed string seemed like a good way to make this
    happen.  So, I created one with its NSParagraphStyleAttributeName set
    to a style with my desired line breaking mode.  Then, I just called
    setAttributedStringValue: on the NSTextField.  This didn't work,
    though, and I then tried changing the setWraps attribute of the text
    field in case that was the problem.  The text field does handle
    string attributions correctly (such as fonts) but seems to ignore the
    paragraph style's line break mode.

    For reference, Apple has a sample code listing in an article about
    truncating strings, <http://developer.apple.com/documentation/Cocoa/
    Conceptual/Rulers/Tasks/TruncatingStrings.html
    >, which is very
    similar to mine.

    Is a subclass of NSTextField or NSTextFieldCell required to
    accomplish this, since handing it an attributed string doesn't seem
    to be enough?  (Or am I missing something really simple - I hate when
    that happens!)

    Thanks for your help,
    Sean
  • On Sep 28, 2006, at 7:23 PM, Sean Murphy wrote:

    > I'm trying to determine the best way to enable an NSTextField to
    > truncate its stringValue (using one of the NSLineBreakMode options)
    > if that value is too long and won't entirely fit into the field's
    > frame.  My text field has been sized in Interface Builder so that
    > it will shrink as the window around it is made smaller.
    >
    > Before you hit reply: I realize this is very simple on 10.4 or
    > later, with a message to a cell's setWraps:YES and
    > setLineBreakMode:.  IB also allows both of these attributes to be
    > set easily.
    >
    > I'm in need of a solution that will work on 10.3.9+, however.  The
    > setLineBreakMode: method was added to the AppKit in Tiger <http://
    > developer.apple.com/releasenotes/Cocoa/AppKit.html>.  The archives
    > and other usual resources, for the most part, only turned up ideas
    > about adding this behavior to text inside an NSTableView.
    >
    > Using an attributed string seemed like a good way to make this
    > happen.  So, I created one with its NSParagraphStyleAttributeName
    > set to a style with my desired line breaking mode.  Then, I just
    > called setAttributedStringValue: on the NSTextField.  This didn't
    > work, though, and I then tried changing the setWraps attribute of
    > the text field in case that was the problem.  The text field does
    > handle string attributions correctly (such as fonts) but seems to
    > ignore the paragraph style's line break mode.

    I was able to accomplish non-10.4 truncating by subclassing
    NSTextFieldCell and drawing the attributed string (with a
    lineBreakStyle attribute) myself:

    @implementation TruncatingTextFieldCell

    - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)
    controlView {
      [[self attributedStringValue] drawInRect:[controlView bounds]];
    }

    @end

    It appears that NSTextFieldCell was never informing its attributed
    string when it wasn't able to fit entirely inside the parent control
    view, meaning the attributed string was drawing in a rect larger than
    the controlView's bounds.

    Even though I'm kinda having a conversation with myself here, I sent
    this in-case anyone was curious :)  If anyone has a better way, or
    I'm forgetting something, please let me know.

    - Sean
previous month september 2006 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Go to today