gpt4 book ai didi

objective-c - 在 Cocoa 中更改 NSTableView 中的突出显示颜色?

转载 作者:太空狗 更新时间:2023-10-30 03:17:39 25 4
gpt4 key购买 nike

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

我尝试子类化 NSTableview 并重写方法

- (void)highlightSelectionInClipRect:(NSRect)clipRect

但这并没有帮助。

如何解决这个问题?

最佳答案

我正在使用它,到目前为止效果很好:

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

关于objective-c - 在 Cocoa 中更改 NSTableView 中的突出显示颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7038709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com