« Welcome to my blog! | Main | rail unicycle riding at East Cliff Rd, Santa Cruz, CA (aka: 'the hook') »
June 10, 2005
Tooltips for NSTableView cell's in Tiger
At WWDC, I quickly mentioned how easy it is to add tooltip's to an NSCell for an NSTableView/NSOutlineView.
Here is a quick snippet of code on how to do this only if the text doesn't fill up the entire cell:
- (NSString *)tableView:(NSTableView *)tv toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tc row:(int)row mouseLocation:(NSPoint)mouseLocation {
if ([cell isKindOfClass:[NSTextFieldCell class]]) {
if ([[cell attributedStringValue] size].width > rect->size.width) {
return [cell stringValue];
}
}
return nil;
}
You will obviously have to set the delegate for the tableview to be whatever class implements the above method, and this will only work on Tiger. But, it is REALLY easy to do.
Technorati Tags: Cocoa
Posted by corbin at June 10, 2005 03:32 PM
Comments
I realize the text is cut off. I need to work on my style sheet. If anyone has a good one for Moveable Type blogs, let me know!
Posted by: corbin at June 10, 2005 03:43 PM
It doesn't work. It is never called for my outline view. :o(
Posted by: anon at June 10, 2005 04:27 PM
you could wrap the code around. otherwise the signature will be rea
418
lly wide and make us scroll horizontally. (or maybe its a way to get us to buy 30 inch display's)
Posted by: GeorgeJ at June 11, 2005 09:58 AM
Ok well darn it, I was hoping you didn't realize your text was cut off because the irony was delicious and I was hoping to be the first to point it out.
In case it's not clear what I'm talking about: The tip was regarding how to show the full text of a cell which is too small to display its content, lol, and yes we couldn't see the full content of your tip because the page wasn't wide enough. I love things like this. In fact I keep a list of them, like: a picture of a picture, the weight of a scale, or, one of my favorites: when someone tells you that you say "I'm sorry" too often and it annoys them, you respond..."I'm sorry".
Posted by: Larry Gerndt at June 11, 2005 11:49 PM
"It doesn't work. It is never called for my outline view. :o("
It won't work for an OutlineView. You must use:
- (NSString *)outlineView:(NSOutlineView *)ov toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tc item:(id)item mouseLocation:(NSPoint)mouseLocation;
Posted by: corbin at June 13, 2005 09:00 AM
"It won't work for an OutlineView. You must use"
Yes, I know that. I copied that from the header of NSOutlineView even, but it is simply never called. What else needs to be done to make this work? I'm sure the class that implements it is set as delegate for the outline view since I see the outline. Just no tooltip ever.
Posted by: anon at June 14, 2005 09:00 AM
Hi anon -- all I can suggest is making sure your delegate is setup correctly. Try posting your issues to the cocoa-dev group list.
thanks!
corbin
Posted by: corbin at June 15, 2005 02:20 PM
"all I can suggest is making sure your delegate is setup correctly."
Oh, I'm not very bright. I missed that "data source" is not the same as "delegate". Now it works fine. Thanks!
This was a great hint since I just needed this, but do I have any control on the placement of the tooltip? Since in your example (and how I want to use it) the tooltip is intended to expand clipped text it should be placed exactly above this text – like the Finder does it for clipped file names in list view. However it is shown somewhere to the bottom right.
Posted by: anon at June 16, 2005 04:42 AM
It even works on my MacMini :)
Posted by: Weyert de Boer at June 16, 2005 05:15 PM