Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Truncating the string value of an NSTextField, in a Panther compatible way. (From: Sean Murphy <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.