objective-c - 自定义 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];
我是这样的:
如您所见,菜单项标题在上图中不可见。提前致谢!
解决方案
推荐阅读
- angular - 取消订阅 Rxjs Observables
- python - 删除行中的值,Pandas DataFrame
- ios - App Clip 可以在 iOS Cocoapods 项目中使用吗?
- c# - 如何使用 JWT Token 管理用户身份?
- c++ - 在没有模板参数的情况下调用模板函数
- r - 在 R 中将日期偏移一个月
- python - “scipy.optimize.least_squares”源码中的平方操作“fun**2”在哪里?
- tensorflow - 模型信息如何发送到 keras 模型
- amazon-web-services - AWS Create Prod 环境源自现有的开发环境配置
- docusignapi - 如何从嵌入式应用程序接收 returnurl 事件?