简体   繁体   中英

Custom selection on NSOutlineView and NSCell (NSTextFieldCell)

Hello developers around the world :) Today I have a question about the NSOutlineView customisation, the environment is: -OS X 10.7+ - an Outline view with subclassed NSTextFieldCell

the problem: I want customise the colour end the gradient for the cell selection, my first approach was override the method

-(NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

in the NSTextFieldCell subclass, but the result is this:

在此处输入图片说明

The gradient selection still remain blu and the label background become to the returned colour. How to change the entire cell selection colour (with the gradient?)


UPDATE:

After subclassing the NSOutlineView and override the method:

- (void)highlightSelectionInClipRect:(NSRect)clipRect
    {
        NSRange        aVisibleRowIndexes  = [self rowsInRect:clipRect];
        NSIndexSet *    aSelectedRowIndexes = [self selectedRowIndexes];
        NSUInteger aRow = aVisibleRowIndexes.location;
        NSUInteger anEndRow = aRow + aVisibleRowIndexes.length;

        NSColor *aColor = MW_MENU_SELECTED_CELL_GB;

        [aColor set];

        // draw highlight for the visible, selected rows
        for (aRow; aRow < anEndRow; aRow++)
        {
            if([aSelectedRowIndexes containsIndex:aRow])
            {
                NSRect aRowRect = NSInsetRect([self rectOfRow:aRow], 2, 1);
                NSRectFill(aRowRect);
            }
        }
    }

Now I have the right behaviour, but without gradient, how to add the gradient (or a background image to the cell?)

I had no luck overriding highlightSelectionInClipRect: . Instead I overrode drawRow:clipRect: . Something like this:

- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect
{
    if ([self isRowSelected: rowIndex])
    {
        [self fillWithHighlightColor: [self rectOfRow: rowIndex]];
    }
    [super drawRow: rowIndex clipRect: clipRect];
}

where fillWithHighlightColor: is my own code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM