自定义NSMenu项目标题在可可应用中可见

时间:2018-09-04 13:55:48

标签: objective-c macos cocoa menuitem nsmenu

我要创建自定义NSMenu项。 首先,我为菜单项创建了一个视图,然后又将自定义视图设置为菜单项。

-(void) drawRect:(NSRect)dirtyRect
{  
NSRect fullBounds = [self bounds];
fullBounds.size.height += 4;
[[NSBezierPath bezierPathWithRect:fullBounds] setClip];
[[NSColor blueColor] set];
NSRectFill( fullBounds );
}

CGFloat menuItemHeight = 32;
NSRect viewRect = NSMakeRect(0, 0, 1, menuItemHeight);
NSView *menuItemView = [[FullMenuItemView alloc] initWithFrame:viewRect];
menuItemView.autoresizingMask = NSViewWidthSizable;

NSMenuItem *unlockAllMenuItem = [[NSMenuItem alloc] initWithTitle:@"UnlockAll" action:@selector(unlockAllMenuItem_Clicked:) keyEquivalent:@""];
[unlockAllMenuItem setView:menuItemView];

unlockAllMenuItem.tag = MenuItemtagUnlockAllMenuItem;
unlockAllMenuItem.target = self;

NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.alignment = NSTextAlignmentCenter;
unlockAllMenuItem.attributedTitle = [[NSAttributedString alloc] initWithString:unlockAllMenuItem.title attributes:@{
                                                                                                                    NSForegroundColorAttributeName:NSColor.redColor,
                                                                                                                    NSFontAttributeName:[NSFont systemFontOfSize:14.0],
                                                                                                                    NSParagraphStyleAttributeName:style,                                                                                                                        }];
[_menu addItem:unlockAllMenuItem];

我越来越像这样:

enter image description here

菜单项标题在上图中不可见,如您所见。   预先感谢!

0 个答案:

没有答案