
圖中黑色的菜單就是UIMenuController
默認(rèn)已經(jīng)支持UIMenuController的控件:
- UITextField
- UITextView
- UIWebView
讓其他控件也支持UIMenuController(比如UILabel)
- 自定義UILabel,讓其成為第一響應(yīng)者,并添加點(diǎn)擊手勢
- (void)setUp {
// 1.設(shè)置label可以交互
self.userInteractionEnabled = YES;
// 2.添加點(diǎn)擊手勢
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)]];
}
- (void)click {
// 3.設(shè)置label為第一響應(yīng)者,只有成為響應(yīng)者才能夠?qū)enuController顯示在其上面
[self becomeFirstResponder];
// 4.初始化UIMenuController
UIMenuController *menuController = [UIMenuController sharedMenuController];
// 5.設(shè)置UIMenuController顯示的位置
// targetRect : 將要顯示所在label的frame;
// view : targetRect所在的坐標(biāo)系參照物(父view或self)
[menuController setTargetRect:self.frame inView:self.superview];
// [menuController setTargetRect:self.bounds inView:self];作用同上
// 6.顯示UIMenuController
[menuController setMenuVisible:YES animated:YES];
}
- 重寫2個(gè)方法
/** * 讓label有資格成為第一響應(yīng)者 */
- (BOOL)canBecomeFirstResponder{
return YES;
}
/** * label能執(zhí)行哪些操作(比如copy, paste等等)
* @return YES:支持這種操作
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) return YES; return NO;
}
- 實(shí)現(xiàn)各種操作方法
- (void)cut:(UIMenuController *)menu {
// 將自己的文字復(fù)制到粘貼板
[self copy:menu];
// 清空文字
self.text = nil;
}
- (void)copy:(UIMenuController *)menu{
// 將自己的文字復(fù)制到粘貼板
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = self.text;
}
- (void)paste:(UIMenuController *)menu{
// 將粘貼板的文字 復(fù)制 到自己身上
UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = board.string;
}
自定義UIMenuController內(nèi)部的Item(以cell為例)
- 點(diǎn)擊cell顯示/隱藏MenuController
- 由于手動(dòng)添加的MenuItem默認(rèn)觸發(fā)控制器中的方法,所以將MenuController的顯示/隱藏,添加MenuItem寫到控制器中(上面Label的例子是讓控件支持系統(tǒng)自帶的一些MenuItem,現(xiàn)在講的是如何支持自己定義的一些MenuItem)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 點(diǎn)擊cell彈出UIMenuController
// 1.如果menuController已經(jīng)在顯示,就隱藏他
// 注意,如果有一個(gè)cell正在顯示menuController,這時(shí)再點(diǎn)擊另外一個(gè)cell,上一個(gè)cell的menuController會(huì)消失,當(dāng)前點(diǎn)擊cell會(huì)顯示,這是因?yàn)樯弦粋€(gè)cell不再是第一響應(yīng)者了,menuController會(huì)自動(dòng)釋放
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController.isMenuVisible) {
[menuController setMenuVisible:NO animated:YES];
}else {
// 2.顯示MenuController
// 先設(shè)置cell為第一響應(yīng)者,同時(shí)不要忘記在cell中重寫canBecomeFirstResponder和canPerformAction:withSender:
JCMTopicCommentCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell becomeFirstResponder];
// 添加menuItem
UIMenuItem *item01 = [[UIMenuItem alloc]initWithTitle:@"頂" action:@selector(ding:)];
UIMenuItem *item02 = [[UIMenuItem alloc]initWithTitle:@"回復(fù)" action:@selector(response:)];
UIMenuItem *item03 = [[UIMenuItem alloc]initWithTitle:@"舉報(bào)" action:@selector(report:)];
menuController.menuItems = @[item01,item02,item03];
// 設(shè)置menuControoler顯示位置
CGRect showRect = CGRectMake(cell.x, cell.y + cell.height/2, cell.width, cell.height);
[menuController setTargetRect:showRect inView:cell.superview];
// 顯示menuController [menuController setMenuVisible:YES animated:YES];
}
}
- 處理方法的實(shí)現(xiàn)
#pragma mark - MenuController處理
// MenuController手動(dòng)添加的item的方法實(shí)現(xiàn)必須放在controller中
- (void)ding:(UIMenuController *)menu {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%s %@", __func__, [self commentInIndexPath:indexPath].content);
}
- (void)response:(UIMenuController *)menu {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%s %@", __func__, [self commentInIndexPath:indexPath].content);
}
- (void)report:(UIMenuController *)menu {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%s %@", __func__, [self commentInIndexPath:indexPath].content);
}
- cell中實(shí)現(xiàn)兩個(gè)方法
// 是否可以成為第一響應(yīng)者
- (BOOL)canBecomeFirstResponder {
return YES;
}
// 是否可以執(zhí)行哪些方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return NO;
}```