Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom NSTableCellView labels not changing text color when selected

I have a custom NSTableCellView with 3 textfields, 1 that came along and 2 others that i created myself. Here's the problem:
enter image description here

The textfields' text color stays the same even when i click on the row. I've tried to implement a code i found out by googling but it doesn't work. My Custom NSTableCellView code is:

- (void)drawRect:(NSRect)dirtyRect{
    NSColor *color = [NSColor colorWithCalibratedRed:(26/255.0) green:(26/255.0) blue:(26/255.0) alpha:1.0];
    [self.textField setTextColor:color];

    color = [NSColor colorWithCalibratedRed:(102/255.0) green:(102/255.0) blue:(102/255.0) alpha:1.0];
    [_lbl1 setTextColor:color];
    [_lbl2 setTextColor:color];
}

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
    NSColor *color = (backgroundStyle == NSBackgroundStyleDark) ? [NSColor windowBackgroundColor] : [NSColor controlShadowColor];
    self.textField.textColor = color;
    self.lbl1.textColor = color;
    self.lbl2.textColor = color;
    [super setBackgroundStyle:backgroundStyle];
}

What can i do to make the labels' text color white when the user clicks on them?

like image 840
Pedro Vieira Avatar asked Oct 20 '12 15:10

Pedro Vieira


3 Answers

Actually, overriding setBackgroundStyle on NSTableViewCell has worked perfectly for me, at least on OS X 10.8. It is updated on selection events and on window activation/deactivation.

Here's my custom cell impl — as trivial as it can get:

@implementation RuntimeInstanceCellView

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
    [super setBackgroundStyle:backgroundStyle];
    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor darkGrayColor] : [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]);
//    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor blackColor] : [NSColor whiteColor]);
}

@end
like image 62
Andrey Tarantsov Avatar answered Oct 23 '22 21:10

Andrey Tarantsov


Expanding on the accepted answer, in Swift 2.0 the process is slightly different. Override the backgroundStyle property of your NSTableCellView subclass to add a didSet property observer:

class CustomTableCellView: NSTableCellView {

    @IBOutlet weak var detailTextField: NSTextField!

    override var backgroundStyle: NSBackgroundStyle {
        didSet {
            if self.backgroundStyle == .Light {
                self.detailTextField.textColor = NSColor.controlTextColor()
            } else if self.backgroundStyle == .Dark {
                self.detailTextField.textColor = NSColor.alternateSelectedControlTextColor()
            }
        }
    }

}
like image 22
Andrew Avatar answered Oct 23 '22 21:10

Andrew


And for Swift 3 & 4 (isn’t this fun?):

override var backgroundStyle: NSView.BackgroundStyle {
    didSet {
        if self.backgroundStyle == .light {
            self.detailTextField.textColor = NSColor.controlTextColor
        } else if self.backgroundStyle == .dark {
            self.detailTextField.textColor = NSColor.alternateSelectedControlTextColor
        }
    }
}
like image 35
John Scalo Avatar answered Oct 23 '22 21:10

John Scalo