Re: Advice on using NSCell
Re: Advice on using NSCell
- Subject: Re: Advice on using NSCell
- From: James Bucanek <email@hidden>
- Date: Fri, 3 Mar 2006 14:19:33 -0700
Keith Wilson wrote on Friday, March 3, 2006:
>The only way I have found to do this is to use willDisplayCell as
>follows
>
>-(void)tableView:(NSTableView *)aTableView willDisplayCell:(id)cell
>forTableColumn:(NSTableColumn *)aTableColumn row:(int)row
>{
> if(row < 0)
> return;
> //NSParameterAssert((row >= 0) && (row < [array count]));
> NSFontManager *fm = [NSFontManager sharedFontManager];
> NSFont *font = [cell font];
> if([array objectAtIndex:row] thing] == whatever) && [cell == ,,,,))
> [cell setFont:[fm convertFont:font toHaveTrait:NSBoldFontMask]];
> else if(......)
> etc
> etc
>}
Thanks Keith, but it doesn't seem to be working for me. I can alter the font, but I can't seem to change the color.
Can someone tell me if NSCell overrides the color attributes attached to the NSFont passed to -[NSCell setFont:] so I can stop banging my head against the wall?
I have used the following code to change between a bold and a regular font, between two font faces, and between two font sizes. That all works. But no matter how I specify the font color, I don't get any traction. Of course, I also harbor the suspicion that I'm specifying the color the wrong way...
Here's the code I wrote (this is my NSTableView delegate:
-------------------------
@implementation LayerTableDelegate
- (id)init
{
if ( (self=[super init]) != nil )
{
// Get the default (normal) font
blackFont = [[NSFont controlContentFontOfSize:0.0] retain];
// Use the normal font as a prototype to create a grey version
NSMutableDictionary* greyAttributes = [NSMutableDictionary dictionaryWithDictionary:[[blackFont fontDescriptor] fontAttributes]];
[greyAttributes setObject:[NSColor colorWithDeviceWhite:0.75 alpha:1.0] forKey:NSFontColorAttribute];
//[greyAttributes setObject:@"Red" forKey:NSForegroundColorAttributeName];
greyFont = [[NSFont fontWithDescriptor:[NSFontDescriptor fontDescriptorWithFontAttributes:greyAttributes] size:[blackFont pointSize]] retain];
}
return (self);
}
- (void)dealloc
{
[blackFont release];
[greyFont release];
[super dealloc];
}
- (void)tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(int)rowIndex
{
#pragma unused(tableColumn)
LayerTableDataSource* dataSource = (LayerTableDataSource*)[tableView dataSource];
// Get the row object for this cell
LayerTableRow* row = [dataSource row:rowIndex];
if (row->includesSelectedVolume)
{
[cell setFont:blackFont];
}
else
{
[cell setFont:greyFont];
}
}
@end
-------------------------
Footnote: I think I've figured out another way of doing this, but it stills seems to be more work that I should have to do[1]. It occurred to me that I could create a kind of "filter" NSFormatter that reused any of the formatters I've already written to do the object->string convertion, then turn that string into a NSAttributedString with the desired color. I wouldn't have to mix my data and view models, and I don't have to throw away any of the formatters I've already written or reimplement any part of NSCell. I just have to insert this "filter" formatter into every column of the table, but that's not so bad as some of the alternatives.
James
[1] Programmers are such whiners.
--
James Bucanek
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden