【问题标题】:Custom selection on NSOutlineView and NSCell (NSTextFieldCell)NSOutlineView 和 NSCell (NSTextFieldCell) 上的自定义选择
【发布时间】:2013-08-16 15:49:46
【问题描述】:

世界各地的开发者您好 :) 今天有个关于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);
            }
        }
    }

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

【问题讨论】:

    标签: objective-c macos cocoa nsoutlineview nscell


    【解决方案1】:

    我没有运气覆盖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: 是我自己的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多