在Cocoa中更改NSTableView中的突出显示颜色? [英] Change highlighting color in NSTableView in Cocoa?

查看:160
本文介绍了在Cocoa中更改NSTableView中的突出显示颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Cocoa应用程序,遇到了突出显示的问题。 MAC OS X应用程序中的标准高亮颜色是蓝色的,但它不适合我的应用程序,因为由于设计概念,我需要一个绿色的高亮显示。



尝试子类化NSTableview和覆盖方法

   - (void)highlightSelectionInClipRect:(NSRect)clipRect 


如果任何人知道如何解决这个问题

解决方案



<我使用这个,到目前为止工作完美:

   - (void)highlightSelectionInClipRect:(NSRect)theClipRect 
{

//这个方法要求我们为
绘制hightlight //所有在ClipRect中可见的选定行

// 1.获取当前可见的行索引范围
// 2.获取所选行的列表
// 3.遍历可见行并选择它们的索引
// 4.绘制我们在该行的rect中的自定义高亮。

NSRange aVisibleRowIndexes = [self rowsInRect:theClipRect];
NSIndexSet * aSelectedRowIndexes = [self selectedRowIndexes];
int aRow = aVisibleRowIndexes.location;
int anEndRow = aRow + aVisibleRowIndexes.length;
NSGradient * gradient;
NSColor * pathColor;

//如果视图是聚焦的,使用高亮颜色,否则使用离焦高亮颜色
if(self == [[self window] firstResponder]&& [NSGradient alloc] initWithColorsAndLocations:
[NSColor colorWithDeviceRed:(float)62] [b] [b] [b] / 255 green:(float)133/255 blue:(float)197/255 alpha:1.0],0.0,
[NSColor colorWithDeviceRed:(float)48/255 green: float)152/255 alpha:1.0],1.0,nil] retain]; // 160 80

pathColor = [[NSColor colorWithDeviceRed:(float)48/255 green:(float)95/255 blue:(float)152/255 alpha:1.0] retain];
}
else
{
gradient = [[[NSGradient alloc] initWithColorsAndLocations:
[NSColor colorWithDeviceRed:(float)190/255 green: 255 blue:(float)150/255 alpha:1.0],0.0,
[NSColor colorWithDeviceRed:(float)150/255 green: ,1.0,nil] retain];

pathColor = [[NSColor colorWithDeviceRed:(float)150/255 green:(float)150/255 blue:(float)150/255 alpha:1.0] retain];
}

//为可见的选定行绘制突出显示
for(aRow; aRow< anEndRow; aRow ++)
{
if aSelectedRowIndexes containsIndex:aRow])
{
NSRect aRowRect = NSInsetRect([self rectOfRow:aRow],1,4); //首先是水平的,第二个是垂直的
NSBezierPath * path = [NSBezierPath bezierPathWithRoundedRect:aRowRect xRadius:4.0 yRadius:4.0]; //6.0
[path setLineWidth:2];
[pathColor set];
[path stroke];

[gradient drawInBezierPath:path angle:90];
}
}
}


I am developing a Cocoa application and encountered a problem with highlighting. Standard highlighting color in MAC OS X applications is blue, but it doesn't suit my app, since because of design concepts, I need a green color for highlighting.

I tried to subclass NSTableview and override method

- (void)highlightSelectionInClipRect:(NSRect)clipRect

but it didn' help.

If anyone knows how to fix this problem help me please, I would be very thankful.

Artem

解决方案

I am using this, and so far works perfectly:

- (void)highlightSelectionInClipRect:(NSRect)theClipRect
{

        // this method is asking us to draw the hightlights for 
        // all of the selected rows that are visible inside theClipRect

        // 1. get the range of row indexes that are currently visible
        // 2. get a list of selected rows
        // 3. iterate over the visible rows and if their index is selected
        // 4. draw our custom highlight in the rect of that row.

    NSRange         aVisibleRowIndexes = [self rowsInRect:theClipRect];
    NSIndexSet *    aSelectedRowIndexes = [self selectedRowIndexes];
    int             aRow = aVisibleRowIndexes.location;
    int             anEndRow = aRow + aVisibleRowIndexes.length;
    NSGradient *    gradient;
    NSColor *       pathColor;

        // if the view is focused, use highlight color, otherwise use the out-of-focus highlight color
    if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow])
    {
        gradient = [[[NSGradient alloc] initWithColorsAndLocations:
                     [NSColor colorWithDeviceRed:(float)62/255 green:(float)133/255 blue:(float)197/255 alpha:1.0], 0.0, 
                     [NSColor colorWithDeviceRed:(float)48/255 green:(float)95/255 blue:(float)152/255 alpha:1.0], 1.0, nil] retain]; //160 80

        pathColor = [[NSColor colorWithDeviceRed:(float)48/255 green:(float)95/255 blue:(float)152/255 alpha:1.0] retain];
    }
    else
    {
        gradient = [[[NSGradient alloc] initWithColorsAndLocations:
                     [NSColor colorWithDeviceRed:(float)190/255 green:(float)190/255 blue:(float)190/255 alpha:1.0], 0.0, 
                     [NSColor colorWithDeviceRed:(float)150/255 green:(float)150/255 blue:(float)150/255 alpha:1.0], 1.0, nil] retain];

        pathColor = [[NSColor colorWithDeviceRed:(float)150/255 green:(float)150/255 blue:(float)150/255 alpha:1.0] retain];
    }

        // draw highlight for the visible, selected rows
    for (aRow; aRow < anEndRow; aRow++)
    {
        if([aSelectedRowIndexes containsIndex:aRow])
        {
            NSRect aRowRect = NSInsetRect([self rectOfRow:aRow], 1, 4); //first is horizontal, second is vertical
            NSBezierPath * path = [NSBezierPath bezierPathWithRoundedRect:aRowRect xRadius:4.0 yRadius:4.0]; //6.0
                [path setLineWidth: 2];
                [pathColor set];
                [path stroke];

            [gradient drawInBezierPath:path angle:90];
        }
    }
}

这篇关于在Cocoa中更改NSTableView中的突出显示颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆