UIMenuController須知
-
官方文檔:
根據官方文檔的描述可知這是一個單例模式,能實現剪切,復制,粘貼,選著,全選和刪除的功能。這個在ios里面也是常用的。
- 默認情況下, 有以下控件已經支持UIMenuController
- UITextField
- UITextView
- UIWebView
-
實現的效果如下:
默認情況下顯示的是英文,如果要顯示中文的話需要進行如下設置:
-
然后就可以顯示中文了:
讓其他控件也支持UIMenuController(比如UILabel)
- 自定義UILabel.
@interface LMHLabel : UILabel
- 設置UILabel允許交互
self.userInteractionEnabled = YES;
- UILabel添加手勢,并添加監聽方法。
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];
- 在監聽方法中創建UIMenuController的menu,添加需要相應的菜單item
// 讓label成為第一響應者
[self becomeFirstResponder];
//獲得菜單 - 單例模式
UIMenuController * menu = [UIMenuController sharedMenuController];
// 添加MenuItem
UIMenuItem* ding = [[UIMenuItem alloc] initWithTitle:@"頂" action:@selector(ding:)];
UIMenuItem* replay = [[UIMenuItem alloc] initWithTitle:@"回復" action:@selector(reply:)];
UIMenuItem* report = [[UIMenuItem alloc] initWithTitle:@"舉報" action:@selector(report:)];
menu.menuItems = @[ding, replay, report];
- 顯示menu - 設置顯示的位置并顯示
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
- 重寫兩個方法實現響應
-
canBecomeFirstResponder
讓label具備成為第一響應者的資格 -
canPerformAction:(SEL)action withSender:(id)sender
通過第一響應者的這個方法告訴UIMenuController可以顯示什么內容
-
- (BOOL)canBecomeFirstResponder{
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(ding:) ||
action == @selector(reply:) ||
action == @selector(report:)) return YES;
return NO;
}
需要注意的地方
- 在設置menu的位置的時候
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView
方法里面的參數:- targetRect: MenuController需要指向的矩形框
- targetView: targetRect會以targetView的左上角為坐標原點
- 只有將uilabel設置為第一響應者之后才會響應相應的時時間
- 另外menu的箭頭也有幾種樣式,可以直接進行設置
menu.arrowDirection = UIMenuControllerArrowUp;
typedef NS_ENUM(NSInteger, UIMenuControllerArrowDirection) {
UIMenuControllerArrowDefault, // up or down based on screen location
UIMenuControllerArrowUp NS_ENUM_AVAILABLE_IOS(3_2),
UIMenuControllerArrowDown NS_ENUM_AVAILABLE_IOS(3_2),
UIMenuControllerArrowLeft NS_ENUM_AVAILABLE_IOS(3_2),
UIMenuControllerArrowRight NS_ENUM_AVAILABLE_IOS(3_2),
} __TVOS_PROHIBITED;
- 此外,默認的復制粘貼等方法系統已經實現,如果要使用,就需要在
canPerformAction
里面實現判斷,返回YES表示支持這種操作
/**
* 通過第一響應者的這個方法告訴UIMenuController可以執行哪些操作(比如copy, paste等等)
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ( (action == @selector(copy:) && self.text) // 需要有文字才能支持復制
|| (action == @selector(cut:) && self.text) // 需要有文字才能支持剪切
|| action == @selector(paste:)
|| action == @selector(ding:)
|| action == @selector(reply:)
|| action == @selector(select:)
|| action == @selector(selectAll:)
|| action == @selector(report:)) return YES;
return NO;
}
使用
- 當在xib或者sb里面新建的label的時候,就可以將類寫成自定義的這個類,就能實現自定義的menu菜單
- 完整代碼:
#import "LMHLabel.h"
@implementation LMHLabel
- (void)awakeFromNib{
[super awakeFromNib];
// 添加手勢
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setUp];
}
return self;
}
- (void)setUp{
self.userInteractionEnabled = YES;
//給lable添加手勢
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];
}
- (void)labelClick{
// 讓label成為第一響應者
[self becomeFirstResponder];
//獲得菜單 - 單例模式
UIMenuController * menu = [UIMenuController sharedMenuController];
// 添加MenuItem
UIMenuItem* ding = [[UIMenuItem alloc] initWithTitle:@"頂" action:@selector(ding:)];
UIMenuItem* replay = [[UIMenuItem alloc] initWithTitle:@"回復" action:@selector(reply:)];
UIMenuItem* report = [[UIMenuItem alloc] initWithTitle:@"舉報" action:@selector(report:)];
menu.menuItems = @[ding, replay, report];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
}
- (void)ding:(UIMenuController *)menu{
NSLog(@"%s %@", __func__ , menu);
}
- (void)reply:(UIMenuController *)menu{
NSLog(@"%s %@", __func__ , menu);
}
- (void)report:(UIMenuController *)menu{
NSLog(@"%s %@", __func__ , menu);
}
/**
* 讓label具備成為第一響應者的資格
*/
- (BOOL)canBecomeFirstResponder{
return YES;
}
/**
* 通過第一響應者的這個方法告訴UIMenuController可以顯示什么內容
* return YES : 支持這種操作
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(ding:) ||
action == @selector(reply:) ||
action == @selector(report:)) return YES;
return NO;
}
@end
-
實現效果如下: