 |
 |
Colors in NSOutlineView
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Anyone know how to change text colors in an NSOutlineView? I need to change them dynamically. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
Even better. I will check this out in about 20 minutes.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
-(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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|