Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Colors in NSOutlineView

Colors in NSOutlineView
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
May 12, 2004, 08:28 AM
 
Anyone know how to change text colors in an NSOutlineView? I need to change them dynamically. Thanks!
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
May 12, 2004, 01:44 PM
 
I think the only way to do it is to subclass the cell. Then you can draw the text any way you like.

The way to do that (short version):

- Create a subclass of NSTextFieldCell
- Override - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView in your subclass
- You may be able to do just change the color and then call the superclass method, or you may have to draw the text yourself (easy to do with NSString drawAtPoint:withAttributes:)
- Set the outline view to use your new cell type by getting the desired column (e.g. column = [[outline tableColumns] objectAtIndex:0];) and then saying [column setDataCell:mySubclass]; where mySubclass is an instance of your subclass.

Does that all make sense?
     
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status: Offline
Reply With Quote
May 12, 2004, 01:54 PM
 
in NSTableView the easiest way is to set the cell's text color in the tableview's -willDisplayCell:forTableColumn:Row: delegate method (I think that's what it's called). Since NSOutlineView is an NSTableView subclass, I would bet it would work for outline views too.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
May 12, 2004, 02:01 PM
 
Thanks, I will check this out tonight. I will check out settings the cell's color first and then the other one. Once I figure out what to do, I will post the code. Thanks!
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
May 12, 2004, 02:13 PM
 
Ok here is what Apple's website says. Havent had a chance to code it yet, but this might work...

In willDisplayCell:forTableColumn:Row

NSColor *txtColor = [NSColor redColor];
NSFont *txtFont = [NSFont boldSystemFontOfSize:14];
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:txtFont, NSFontAttributeName, txtColor, NSForegroundColorAttributeName, nil];
NSAttributedString *attrStr = [[[NSAttributedString alloc] initWithString:@"Hello!" attributes:txtDict] autorelease];

[aCell setAttributedStringValue:attrStr];

I will check it out tonight. You can find the source here: http://developer.apple.com/documenta...00074/BBCCFCEH
     
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status: Offline
Reply With Quote
May 12, 2004, 02:51 PM
 
Originally posted by djohnson:
Ok here is what Apple's website says. Havent had a chance to code it yet, but this might work...

In willDisplayCell:forTableColumn:Row

NSColor *txtColor = [NSColor redColor];
NSFont *txtFont = [NSFont boldSystemFontOfSize:14];
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:txtFont, NSFontAttributeName, txtColor, NSForegroundColorAttributeName, nil];
NSAttributedString *attrStr = [[[NSAttributedString alloc] initWithString:@"Hello!" attributes:txtDict] autorelease];

[aCell setAttributedStringValue:attrStr];

I will check it out tonight. You can find the source here: http://developer.apple.com/documenta...00074/BBCCFCEH
http://developer.apple.com/documenta.../setTextColor_

you can find out what class the cell is by calling -className on it in your willDisplayCell delegate method. In the case of NSTableViews it's NSTextFieldCell, so you don't have to bother with creating an attributed string every time you want to change the cell's value.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
May 12, 2004, 04:57 PM
 
Even better. I will check this out in about 20 minutes.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
May 12, 2004, 07:07 PM
 
-(id)outlineView: (NSOutlineView *)ov objectValueForTableColumn: (NSTableColumn *)tableColumn byItem: (id)item {
[[tableColumn dataCell] setTextColor:[NSColor redColor]];
return [item objectForKey:[tableColumn identifier]];
}

If anyone wants to know, this works great. Just change the [NSColor redColor] to the color of your choice! Credit goes to Uncle Skeleton for the lead.
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 13, 2004, 07:00 PM
 
Changing attributes of the table column's data cell may work when done in -outlineView:objectValueForTableColumn:byItem:, but I'd recommend against doing it there. Apple reserves the right to change the way NSOutlineView works internally -- you might find on a future version of Mac OS X that all the cells in your column are red when you intend for only one of them to be. The recommended method to implement for changing cell attributes on a per-item basis is -outlineView:willDisplayCell:forTableColumn:item:.

Also, IIRC another way to accomplish what you're asking for is to return an NSAttributedString instead of an NSString from -outlineView:objectValueForTableColumn:byItem:. Then you don't even have to touch the cell.
Rick Roe
icons.cx | weblog
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:30 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2