首页 > 解决方案 > 自定义 NSMenu 项目标题在 Cocoa 应用程序中可见

问题描述

我想创建自定义 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];

我是这样的:

在此处输入图像描述

如您所见,菜单项标题在上图中不可见。提前致谢!

标签: objective-cmacoscocoamenuitemnsmenu

解决方案


推荐阅读