问题描述
我正在为 NSMenuItem
创建自定义视图。为了在选择时绘制背景,我修改了来自示例。 CustomMenus示例具有:
[[NSColor alternateSelectedControlColor] set];
NSRectFillUsingOperation(dirtyRect,NSCompositeSourceOver);
..我使用selectedMenuItemColor,因为alternateSelectedControlColor是一个纯色, good:
[[NSColor selectedMenuItemColor] set];
NSRectFillUsingOperation(dirtyRect,NSCompositeSourceOver);
使用selectedMenuItemColor更好,但它仍然不完全相同 NSMenuItem
。
这是一个屏幕截图,显示真实选择的 NSMenuItem
左侧和selectedMenuItemColor在右侧的蓝色外观:
您可以看到在真实选择的 NSMenuItem
背景上添加半透明白色渐变叠加。
如何复制真实选择的 NSMenuItem
background?
编辑:适用于Mac OS 10.9.5。
EDIT2:这是石墨外观中的并排比较:
通过试验和错误我想出了一个背景几乎不可区分的背景, code> NSMenuItem 背景中的蓝色和石墨外观:
[NSColor selectedMenuItemColor] set];
NSRectFillUsingOperation(dirtyRect,NSCompositeSourceOver);
if(dirtyRect.size.height> 1){
const NSControlTint currentControlTint = [NSColor currentControlTint] ;
const CGFloat startingOpacity =(NSBlueControlTint == currentControlTint?(CGFloat)0.16:(CGFloat)0.09);
NSGradient * grad = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithWhite :( CGFloat)1.0 alpha:startingOpacity] endingColor:[NSColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)0.0]];
const CGFloat heightMinus1 =(CGFloat)(dirtyRect.size.height - ;
[grad drawFromPoint:NSMakePoint(dirtyRect.origin.x,dirtyRect.origin.y + heightMinus1)toPoint:NSMakePoint(dirtyRect.origin.x,dirtyRect.origin.y + 1)options:0u];
if(NSBlueControlTint == currentControlTint){
[[NSColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)0.1] set];
NSRectFillUsingOperation(NSMakeRect(dirtyRect.origin.x,dirtyRect.origin.y + heightMinus1,dirtyRect.size.width,(CGFloat)1.0),NSCompositeSourceOver);
}
}
这里是并排比较: p>
两个图像的左半部分(80px)显示真实选择的 NSMenuItem
背景,两个图像的右半部分是代码的结果。
I am creating a custom view for an NSMenuItem
. In order to draw the background when selected, I adapted a couple of lines from the CustomMenus sample. The CustomMenus sample has:
[[NSColor alternateSelectedControlColor] set];
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
.. and I am using the selectedMenuItemColor because the alternateSelectedControlColor was a solid color and it did not look very good:
[[NSColor selectedMenuItemColor] set];
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
Using selectedMenuItemColor is better, but it's still not exactly the same as a real selected NSMenuItem
.
Here is a screenshot showing the real selected NSMenuItem
background on the left and the selectedMenuItemColor on the right in the "Blue" appearance:
You can see that there is an additional translucent white gradient overlay on the real selected NSMenuItem
background.
How do I replicate the real selected NSMenuItem
background?
EDIT: This is for Mac OS 10.9.5.
EDIT2: Here is a side-by-side comparison in the "Graphite" appearance:
Through trial and error I came up with the following code that draws a background almost indistinguishable from the real selected NSMenuItem
background in both "Blue" and "Graphite" appearances:
[[NSColor selectedMenuItemColor] set];
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
if (dirtyRect.size.height > 1) {
const NSControlTint currentControlTint = [NSColor currentControlTint];
const CGFloat startingOpacity = (NSBlueControlTint == currentControlTint ? (CGFloat)0.16 : (CGFloat)0.09);
NSGradient *grad = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithWhite:(CGFloat)1.0 alpha:startingOpacity] endingColor:[NSColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)0.0]];
const CGFloat heightMinus1 = (CGFloat)(dirtyRect.size.height - 1);
[grad drawFromPoint:NSMakePoint(dirtyRect.origin.x, dirtyRect.origin.y + heightMinus1) toPoint:NSMakePoint(dirtyRect.origin.x, dirtyRect.origin.y + 1) options:0u];
if (NSBlueControlTint == currentControlTint) {
[[NSColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)0.1] set];
NSRectFillUsingOperation(NSMakeRect(dirtyRect.origin.x, dirtyRect.origin.y + heightMinus1, dirtyRect.size.width, (CGFloat)1.0), NSCompositeSourceOver);
}
}
Here are side-by-side comparisons:
The left halves (80px) of the two images show the real selected NSMenuItem
background and the right halves of the two images are the result of the code.
这篇关于完全匹配所选NSMenuItem的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!