Re: NSTableView and Ellipses--Again
Re: NSTableView and Ellipses--Again
- Subject: Re: NSTableView and Ellipses--Again
- From: Simon Stapleton <email@hidden>
- Date: Sun, 11 Aug 2002 15:05:16 +0200
Hi Buzz
All,
Since I got no responses on my last post about this, I thought I'd post
again--this time with a slightly different question...
As I said in my last message, I am trying to implement the functionality
found in iTunes' table view that causes it to truncate strings which
are too
long to fit within in a column and place ellipses (...) at the end of
the
visible part.
Below is the code I'm using, which I am not very fond of.
Essentially, I
iterate through each character in the string, add the current character
to
the end of an NSMutableAttributedString, and use the size method in that
class to determine if the attributed string has reached a length yet
that
would be too long for the column. If it has, I add the ellipses in the
proper place.
I know that this is an ugly way to do it, and it seems to contribute to
noticeable sluggishness as the user moves through the table view (the
code
is called over and over by the "willDisplayCell" delegate method).
Does anyone have any ideas about a better way to do it? I know it must
be
possible to create *something* more efficient, because iTunes' table
view
doesn't seem to suffer the kind of sluggishness I'm seeing...
Any advice anyone could offer would be most appreciated!
First off, I would tend to put this in a category on NSString, something
like
- (NSString *) truncatedStringWithIndicatorString: (NSString *) aString
fittingTableView: (NSTableView *)
aTableView
tableColumn: (NSTableColumn *) aTableColumn;
which would make your code a lot cleaner where it's being called, and
enable you to make - <ahem> - megabucks - <ahem> - by releasing the
category for free on the internet so other people could use it too.
Changing aString would allow you to use something else in localisations
(or any other case you can think of - there may be an unicode ellipses
character, for example) but it gets away from hardcoding strings into
your code, which is generally a good thing (tm).
As for the implementation - maybe something like this (typed directly
into mail, so treat with caution)
- (NSString *) truncatedStringWithIndicatorString: (NSString *) aString
fittingTableView: (NSTableView *)
aTableView
tableColumn: (NSTableColumn *) aTableColumn
{
NSString * returnedString;
NSCell * cell = [aTableColumn dataCell];
NSDictionary * attributes = [NSDictionary dictionaryWithObject:[cell
font] forKey:@"NSFontAttributeName"];
unsigned int ellipsisWidth = [aString
sizeWithAttributes:attributes].width;
unsigned int desiredWidth = [dataCell
titleRectForBounds:NSMakeRect(0, 0, [aTableColumn width], [aTableView
rowHeight])].size.width;
// Assume our string will fit...
if ([self sizeWithAttributes:attributes].width <= desiredWidth) {
returnedString = [NSString stringWithString:self];
}
else {
// do a binary search until we fit as tightly as possible
BOOL exactFit = NO;
unsigned int numChars = [self length] / 2;
unsigned int index = numChars;
// Assign this string first to deal with pathological case
NSString * choppedString = [self substringToIndex:index];
while ((numChars > 1) && !exactFit) {
unsigned int thisWidth = [choppedString
sizeWithAttributes:attributes].width + ellipsisWidth;
numChars /= 2;
if (thisWidth < desiredWidth)
index += numChars;
else if (thisWidth > desiredWidth)
index -= numChars;
else
exactFit = YES;
choppedString = [self substringToIndex:index];
}
returnedString = [choppedString stringByAppendingString:aString];
}
return returnedString;
}
You may need to mess with the size comparisons, to allow some 'slop' in
the cell.
I'm not sure about the performance boost this will give (if any) - I'm
not sure of the performance of -sizeWithAttributes: and
-substringToIndex: - but the binary search approach _should_ help speed
things up, especially with bigger strings...
I'm actually pretty sure that the performance of -sizeWithAttributes: is
in the same order of magnitude as that of NSAttributedString's -size
method, but have nothing to back that up and am too lazy to check ;-)
Hope this helps.
Simon
--
PGP Key Id : 0x50D0698D
--
Your mouse has moved. You must restart Windows NT for this change to be
recognised.
_______________________________________________
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.