一、引言
NSMenu在Mac桌面軟體開發中往往有3個方面的應用,作為程式的主菜單欄使用,作為視圖郵件菜單使用和作為Dock菜單使用。
二、主應用菜單
使用Xcode建立OX S應用時,可以選擇使用Storyboard。Storyboard裡面會自動建立一個菜單欄,你可以自行在菜單中進行增删改操作,菜單中的Item觸發方法也可以直接與AppDelegate進行關聯,實作自定義的菜單邏輯,如圖:
三:Dock菜單
當一款Mac桌面軟體運作時,會在Dock欄上顯示一個圖示,當在此圖示上點選右鍵時,會出現一個Dock菜單,自定義此Dock菜單也十分容易,直接在AppDelegate中重寫如下方法即可:
-(NSMenu *)applicationDockMenu:(NSApplication *)sender{
NSMenu * menu = [[NSMenu alloc]initWithTitle:@"Menu"];
NSMenuItem * item1 = [[NSMenuItem alloc]initWithTitle:@"菜單1" action:@selector(click) keyEquivalent:@""];
item1.target = self;
NSMenuItem * item2 = [[NSMenuItem alloc]initWithTitle:@"菜單2" action:@selector(click) keyEquivalent:@""];
item2.target = self;
NSMenuItem * item3 = [[NSMenuItem alloc]initWithTitle:@"菜單3" action:@selector(click) keyEquivalent:@""];
NSMenu * subMenu = [[NSMenu alloc]initWithTitle:@"subMenu"];
NSMenuItem * item4 = [[NSMenuItem alloc]initWithTitle:@"菜單4" action:@selector(click) keyEquivalent:@""];
item4.target = self;
[subMenu addItem:item4];
[menu addItem:item1];
[menu addItem:item2];
[menu addItem:item3];
[menu setSubmenu:subMenu forItem:item3];
return menu;
}
效果如下:
四、視圖右鍵彈出菜單
視圖右鍵彈出菜單是基于NSView視圖的,例如:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setMenu:menu];
五、NSMenuItem詳解
NSMenuItem是菜單中的每一個菜單選項對象,其中常用屬性方法如下:
//設定是否啟用使用者快捷鍵
+ (void)setUsesUserKeyEquivalents:(BOOL)flag;
//設定使用者快捷鍵啟用狀态
+ (BOOL)usesUserKeyEquivalents;
//建立一個分割線
+ (NSMenuItem *)separatorItem;
//使用标題,快捷鍵和方法選擇器來對Item進行初始化
- (instancetype)initWithTitle:(NSString *)string action:(nullable SEL)selector keyEquivalent:(NSString *)charCode;
//其所在的菜單對象
@property (nullable, assign) NSMenu *menu;
//其是否有子菜單
@property (readonly) BOOL hasSubmenu;
//子菜單對象
@property (nullable, strong) NSMenu *submenu;
//如果此Item是某個子菜單中的,此屬性擷取與子菜單關聯的父item
@property (nullable, readonly, assign) NSMenuItem *parentItem;
//Item标題
@property (copy) NSString *title;
//富文本标題
@property (nullable, copy) NSAttributedString *attributedTitle;
//是否是分隔線Item
@property (getter=isSeparatorItem, readonly) BOOL separatorItem;
//綁定的快捷鍵
@property (copy) NSString *keyEquivalent;
//快捷鍵類型
/*
typedef NS_OPTIONS(NSUInteger, NSEventModifierFlags) {
NSEventModifierFlagCapsLock = 1 << 16, // Caps lock鍵
NSEventModifierFlagShift = 1 << 17, // shift鍵
NSEventModifierFlagControl = 1 << 18, // control鍵
NSEventModifierFlagOption = 1 << 19, // option鍵
NSEventModifierFlagCommand = 1 << 20, // command鍵
NSEventModifierFlagNumericPad = 1 << 21, // 小鍵盤任意鍵
NSEventModifierFlagHelp = 1 << 22, // 幫助鍵
NSEventModifierFlagFunction = 1 << 23, // 任意功能按鈕
};
*/
@property NSEventModifierFlags keyEquivalentModifierMask;
//Item圖示
@property (nullable, strong) NSImage *image;
//Item狀态
@property NSInteger state;
//開啟狀态下的圖示
@property (null_resettable, strong) NSImage *onStateImage;
//關閉狀态下的圖示
@property (nullable, strong) NSImage *offStateImage;
//混合狀态下的圖示
@property (null_resettable, strong) NSImage *mixedStateImage;
//是否有效
@property (getter=isEnabled) BOOL enabled;
//是否前置
@property (getter=isAlternate) BOOL alternate;
//Item縮進級别
@property NSInteger indentationLevel;
//設定互動響應者
@property (nullable, weak) id target;
//設定互動相應方法
@property (nullable) SEL action;
//設定tag值
@property NSInteger tag;
//是否高亮
@property (getter=isHighlighted, readonly) BOOL highlighted;
//設定是否隐藏
@property (getter=isHidden) BOOL hidden;
//設定提示文本
@property (nullable, copy) NSString *toolTip;
六、NSMenu詳解
//初始化方法
- (instancetype)initWithTitle:(NSString *)title;
//标題
//在所在的互動點彈出菜單
+ (void)popUpContextMenu:(NSMenu*)menu withEvent:(NSEvent*)event forView:(NSView*)view;
+ (void)popUpContextMenu:(NSMenu*)menu withEvent:(NSEvent*)event forView:(NSView*)view withFont:(nullable NSFont*)font;
- (BOOL)popUpMenuPositioningItem:(nullable NSMenuItem *)item atLocation:(NSPoint)location inView:(nullable NSView *)view NS_AVAILABLE_MAC(10_6);
//設定菜單欄是否可見
+ (void)setMenuBarVisible:(BOOL)visible;
+ (BOOL)menuBarVisible;
//父菜單
@property (nullable, assign) NSMenu *supermenu;
//插入Item
- (void)insertItem:(NSMenuItem *)newItem atIndex:(NSInteger)index;
- (NSMenuItem *)insertItemWithTitle:(NSString *)string action:(nullable SEL)selector keyEquivalent:(NSString *)charCode atIndex:(NSInteger)index;
//添加Item
- (void)addItem:(NSMenuItem *)newItem;
- (NSMenuItem *)addItemWithTitle:(NSString *)string action:(nullable SEL)selector keyEquivalent:(NSString *)charCode;
//删除某個位置的Item
- (void)removeItemAtIndex:(NSInteger)index;
//删除Item
- (void)removeItem:(NSMenuItem *)item;
//為某個Item設定子菜單
- (void)setSubmenu:(nullable NSMenu *)menu forItem:(NSMenuItem *)item;
//删除所有Item
- (void)removeAllItems;
//Item數組
@property (readonly, copy) NSArray<NSMenuItem *> *itemArray;
//擷取Item個數
@property (readonly) NSInteger numberOfItems;
//擷取某個位置的Item
- (nullable NSMenuItem *)itemAtIndex:(NSInteger)index;
//擷取某個Item的位置
- (NSInteger)indexOfItem:(NSMenuItem *)item;
- (NSInteger)indexOfItemWithTitle:(NSString *)title;
- (NSInteger)indexOfItemWithTag:(NSInteger)tag;
- (NSInteger)indexOfItemWithSubmenu:(nullable NSMenu *)submenu;
- (NSInteger)indexOfItemWithTarget:(nullable id)target andAction:(nullable SEL)actionSelector;
//根據标題擷取item
- (nullable NSMenuItem *)itemWithTitle:(NSString *)title;
//根據tag擷取Item
- (nullable NSMenuItem *)itemWithTag:(NSInteger)tag;
//重新整理菜單
- (void)update;
//擷取菜單高度
@property (readonly) CGFloat menuBarHeight;
//取消菜單
- (void)cancelTracking;
- (void)cancelTrackingWithoutAnimation;
//擷取高亮的Item
@property (nullable, readonly, strong) NSMenuItem *highlightedItem;
//最小寬度
@property CGFloat minimumWidth;
//尺寸
@property (readonly) NSSize size;
//字型
@property (null_resettable, strong) NSFont *font;