Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCell Custom Highlight

Tags:

cocoa

nscell

I'm trying to subclass NSCell to draw a custom background highlight. The documentation seems to suggest that the overriding highlight:withFrame:inView: should allow me to do this but the method is never called.

Instead, I've overridden drawInteriorWithFrame:inView: which works fine - I can draw what I want in the cell. However, the issue is that I have to draw everything myself, losing the functionality of the type of NSCell I am extending - for example an NSTextFieldCell's ability to display text:

Custom drawn highlighted cell:

enter image description here

However, I just want to redraw the background (the highlight), and retain the ability to use the main functionality of the extended cell:

enter image description here

I could, of course, just draw the text myself too but I'm hoping there is an easier way of doing this.

Any help is much appreciated.

like image 903
JoeR Avatar asked Feb 18 '11 12:02

JoeR


1 Answers

Thanks to the help of @Bavarious I've managed to work it out. My extended NSTextFieldCell class implementation now contains:

-(NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    return nil;
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    if ([self isHighlighted]) {
        // Draw highlight background here
    }

    [super drawInteriorWithFrame:cellFrame inView:controlView];
}

The key is to make sure you return nil for highlightColorWithFrame:inView: to stop drawInteriorWithFrame:inView: drawing a background and yet still calling it to draw the main content (in this case text).

like image 84
JoeR Avatar answered Oct 07 '22 10:10

JoeR