gpt4 book ai didi

cocoa - 自定义 NSMenu 之类的 View 未正确显示 selectedMenuItemColor

转载 作者:行者123 更新时间:2023-12-03 16:15:51 25 4
gpt4 key购买 nike

我编写了自己的类似 NSMenu 的类,以在无边框 NSWindow 中的 NSSearchField 下方显示动态搜索结果。它运行良好,但如果我在 subview 的顶部添加一些填充,则无法正确绘制神奇的 selectedMenuItemColor 。我在容器 View 的顶部放置了一个 5 像素填充来模仿 NSMenu,当我这样做时,蓝色选定的渐变看起来会消失。图片和代码应该可以清楚地说明这一点:

enter image description here

这是我的项目 View 中的drawRect代码(记住这只是一个常规的NSView):

-(void) drawRect:(NSRect)dirtyRect {
CGRect b = self.bounds;
if (selected) {

[NSGraphicsContext saveGraphicsState];

[[NSColor selectedMenuItemColor] set];
NSRectFill( b );

[NSGraphicsContext restoreGraphicsState];
if (textField) {
textField.textColor = [NSColor selectedMenuItemTextColor];
}
} else {
[[NSColor clearColor] set];
NSRectFillUsingOperation(b, NSCompositeSourceOver);
if (textField) {
textField.textColor = [NSColor blackColor];
}
}
}

最佳答案

您必须获取图案相位原点以匹配您的 View 框架。

也就是说,selectedMenuItemColor 实际上是一种图案,而不是颜色,并且该图案旨在以“标准菜单项高度”增量“正确”显示。因为您已经添加了填充,所以现在它没有绘制在“标准”位置。

试试这个:

-(void) drawRect:(NSRect)dirtyRect {
CGRect b = self.bounds;
if (selected) {

NSPoint origin = [self frame].origin;
curContext = [NSGraphicsContext currentContext];
[curContext saveGraphicsState];
[curContext setPatternPhase: origin];

[[NSColor selectedMenuItemColor] set];
NSRectFill( b );

[curContext restoreGraphicsState];
if (textField) {
textField.textColor = [NSColor selectedMenuItemTextColor];
}
} else {
[[NSColor clearColor] set];
NSRectFillUsingOperation(b, NSCompositeSourceOver);
if (textField) {
textField.textColor = [NSColor blackColor];
}
}
}

关于cocoa - 自定义 NSMenu 之类的 View 未正确显示 selectedMenuItemColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683192/

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