Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField in NSTableCellView

I have a view based NSTableView with a custom NSTableCellView. This custom NSTableCellView has several labels (NSTextField). The whole UI of the NSTableCellView is built in IB.

The NSTableCellView can be in a normal state and in a selected state. In the normal state all text labels should be black, in the selected state they should be white.

How can I manage this?

like image 945
burki Avatar asked Feb 05 '12 12:02

burki


1 Answers

Override setBackgroundStyle: on the NSTableCellView to know when the background changes which is what affects what text color you should use in your cell.

For instance:

- (void)setBackgroundStyle:(NSBackgroundStyle)style
{
    [super setBackgroundStyle:style];

    // If the cell's text color is black, this sets it to white
    [((NSCell *)self.descriptionField.cell) setBackgroundStyle:style];

    // Otherwise you need to change the color manually
    switch (style) {
        case NSBackgroundStyleLight:
            [self.descriptionField setTextColor:[NSColor colorWithCalibratedWhite:0.4 alpha:1.0]];
            break;

        case NSBackgroundStyleDark:
        default:
            [self.descriptionField setTextColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
            break;
    }
}

In source list table views the cell view's background style is set to Light, as is its textField's backgroundStyle, however the textField also draws a shadow under its text and haven't yet found exactly what is controlling that / determining that should it happen.

like image 86
Seth Avatar answered Sep 28 '22 16:09

Seth