世界各地的Hello开发人员:)
今天,我对NSOutlineView的自定义有疑问,环境是:
-OS X 10.7+
-具有子类NSTextFieldCell的大纲视图

问题:我想为单元格选择自定义颜色末端的渐变,我的第一种方法是覆盖该方法

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


在NSTextFieldCell子类中,但是结果是这样的:



渐变选择仍然保持为蓝色,标签背景变为返回的颜色。如何更改整个单元格选择颜色(使用渐变?)



更新:

子类化NSOutlineView并重写该方法之后:

- (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);
            }
        }
    }


现在我的行​​为正确,但是没有渐变,如何添加渐变(或将背景图像添加到单元格中?)

最佳答案

我没有运气超越highlightSelectionInClipRect:。相反,我覆盖了drawRow:clipRect:。像这样:

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


其中fillWithHighlightColor:是我自己的代码。

关于objective-c - 自定义选择NSOutlineView和NSCell(NSTextFieldCell),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18277372/

10-14 18:41
查看更多