Gap above NSMenuItem custom view

前端 未结 1 995
遥遥无期
遥遥无期 2020-11-30 01:59

I am using the setView: method on an NSMenuItem to set a custom view. In this custom view there is an image which takes the whole of the view. The

相关标签:
1条回答
  • 2020-11-30 02:43

    Your post is tagged "Objective-C" and "Cocoa", although your sample code is C and Carbon. I assume you'd prefer a Cocoa solution?

    It's actually pretty simple in Cocoa. The only trick is learning how to draw outside the lines. :-)

    @interface FullMenuItemView : NSView
    @end
    
    @implementation FullMenuItemView
    - (void) drawRect:(NSRect)dirtyRect
    {
        NSRect fullBounds = [self bounds];
        fullBounds.size.height += 4;
        [[NSBezierPath bezierPathWithRect:fullBounds] setClip];
    
        // Then do your drawing, for example...
        [[NSColor blueColor] set];
        NSRectFill( fullBounds );
    }
    @end
    

    Use it like this:

    CGFloat menuItemHeight = 32;
    
    NSRect viewRect = NSMakeRect(0, 0, /* width autoresizes */ 1, menuItemHeight);
    NSView *menuItemView = [[[FullMenuItemView alloc] initWithFrame:viewRect] autorelease];
    menuItemView.autoresizingMask = NSViewWidthSizable;
    
    yourMenuItem.view = menuItemView;
    
    0 讨论(0)
提交回复
热议问题