Re: tableView column ellipses
Re: tableView column ellipses
- Subject: Re: tableView column ellipses
- From: Ambroise Confetti <email@hidden>
- Date: Fri, 21 Nov 2003 18:32:31 +0100
Le 21 nov. 03, ` 03:37, Evan Gross a icrit :
>
On 11/19/03 10:32 PM, "Kurt Marek" <email@hidden> wrote:
>
>
> How do I get truncated text in a column of an NSTableView to show
>
> ellipses when the column isn't wide enough to show the full text? For
>
> an example of the behavior I want, see iTunes. Is this a simple
>
> setting
>
> that I haven't located or is it done programmatically?
>
>
>
> Kurt
>
>
If you need something that works on 10.2 and 10.3, you could try using
>
some
>
source I posted to CocoaDev:
Here is an easier (but slower) solution which I'm using in my software
Cellulo. It behaves as follows:
- If the string fits, don't change it.
- It it doesn't fit, reduce the kerning.
- If it still doesn't fit AND the available width is greater than
kMinWidthForEllipsis, replace the 3 middle characters enough times by
an ellipsis to make it fit.
I didn't try to profile the code, but I think it's slow because calling
-[NSAttributedString size] takes some time.
As usual, there's no warranty. There are also some hardcoded values
which you may want to change (you will first have to guess what they
are ;-). It should work with attributed and non-attributed string
values. The string value of the cell should be reset each time it is
drawn (which is the case with table views).
It works with 10.2 and 10.3.
(The header just defines CelluloTextFieldCell as a subclass of
NSTextFieldCell.)
#import "CelluloTextFieldCell.h"
#define kMinWidthForEllipsis (70.f)
static float ellipsisWidth = 0.f;
@implementation CelluloTextFieldCell
+ (void)initialize
{
ellipsisWidth = [[[[NSAttributedString alloc] initWithString:@""]
autorelease] size].width;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView
*)controlView
{
NSMutableAttributedString *string;
int middle;
int initialLength;
NSRange range;
float tooLongBy;
int stopAt;
int step;
NSAttributedString *s;
string = [[self attributedStringValue] mutableCopy];
if( string == nil )
string = [[NSMutableAttributedString alloc] initWithString:[self
stringValue]];
if( [string size].width > cellFrame.size.width-2.f )
{
initialLength = [string length];
[string addAttribute:NSKernAttributeName value:[NSNumber
numberWithFloat:-1.f] range:NSMakeRange( 0.f, initialLength )];
if( cellFrame.size.width > kMinWidthForEllipsis )
{
tooLongBy = [string size].width - cellFrame.size.width + 2.f +
ellipsisWidth;
stopAt = (initialLength - 3.f) / 2.f;
if( tooLongBy > 0.f && stopAt > 0 )
{
step = 2;
middle = initialLength / 2;
do
{
range = NSMakeRange( middle-1-step, 3+2*step );
s = [string attributedSubstringFromRange:range];
step++;
}
while( step < stopAt && [s size].width < tooLongBy );
[string replaceCharactersInRange:range withString:@""];
}
}
}
[self setAttributedStringValue:string];
[string release];
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
@end
Ambroise
http://www.cellulo.info/
ICQ 4508259
AIM atvaark
[demime 0.98b removed an attachment of type text/directory which had a name of Ambroise Confetti.vcf]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.