义NSMenu类似视图不能正确显示selectedMenuIte

义NSMenu类似视图不能正确显示selectedMenuIte

本文介绍了自定义NSMenu类似视图不能正确显示selectedMenuItemColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了我自己的NSMenu类,在无边框NSWindow中的 NSSearchField 下面显示动态搜索结果。它的工作很好,但是如果我在子视图的顶部添加一些填充,不绘制神奇 selectedMenuItemColor 。我把一个5像素填充在容器视图的顶部模仿NSMenu,当我做它蓝色选择渐变看起来。图片&代码应该清楚:





这是我的项目视图内的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];
}
}
}


解决方案



也就是说, 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];
}
}
}


I wrote my own NSMenu like class to show dynamic search results below a NSSearchField in a borderless NSWindow. It's working well but doesn't draw the magic selectedMenuItemColor correctly if I add some padding to the top of the subview. I put a 5 pixel padding at the top of container view to mimic NSMenu and when I do it blue selected gradient looks off. A picture & code should make this clear:

Here is my drawRect code inside my item view (remember this is just a regular 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];
        }
    }
}
解决方案

You have to get the pattern phase origin to match your view frame.

That is, selectedMenuItemColor is actually a pattern, not a color, and that pattern is designed to display "properly" in "standard menu item height" increments. Because you have added the padding, now it is not being drawn at the "standard" location.

Try this:

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

这篇关于自定义NSMenu类似视图不能正确显示selectedMenuItemColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:32