- 默认情况下, 有以下控件已经支持UIMenuController
- UITextField
- UITextView
- UIWebView
- 自定义UILabel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| - (void)setUp { self.userInteractionEnabled = YES; [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lableClick)]]; } - (void)lableClick { [self becomeFirstResponder]; UIMenuController *menuController = [UIMenuController sharedMenuController]; [menuController setTargetRect:self.frame inView:self.superview]; [menuController setMenuVisible:YES animated:YES]; }
|
- 在UILable内重写2个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
- (BOOL)canBecomeFirstResponder { return YES; }
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) return YES;
return NO; }
|
3.实现各种操作方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| - (void)cut:(UIMenuController *)menu { UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = nil; }
- (void)copy:(UIMenuController *)menu { UIPasteboard *board = [UIPasteboard generalPasteboard]; board.string = self.text; }
- (void)paste:(UIMenuController *)menu { UIPasteboard *board = [UIPasteboard generalPasteboard]; self.text = board.string; }
|
- 由于手动添加的MenuItem默认触发控制器中的方法,所以将MenuController的显示/隐藏,添加MenuItem写到控制器
- 添加item
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIMenuController *menuController = [UIMenuController sharedMenuController]; if (menuController.isMenuVisible) { [menuController setMenuVisible:NO animated:YES]; }else { JCMTopicCommentCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell becomeFirstResponder]; UIMenuItem *item01 = [[UIMenuItem alloc]initWithTitle:@"赞" action:@selector(zanClick:)]; UIMenuItem *item02 = [[UIMenuItem alloc]initWithTitle:@"回复" action:@selector(responseClick:)]; UIMenuItem *item03 = [[UIMenuItem alloc]initWithTitle:@"举报" action:@selector(reportClick:)]; menuController.menuItems = @[item01,item02,item03]; CGRect showRect = CGRectMake(cell.x, cell.y + cell.height/2, cell.width, cell.height); [menuController setTargetRect:showRect inView:cell.superview]; } }
|
- cell中实现两个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
- (BOOL)canBecomeFirstResponder { return YES; }
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) return YES; return NO; }
|
- 处理方法的实现
#pragma mark - MenuControllerClick
// MenuController手动添加的item的方法实现必须放在controller中
- (void)zanClick:(UIMenuController *)menu {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%s %@", __func__, [self commentInIndexPath:indexPath].content);
}
- (void)responseClick:(UIMenuController *)menu {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%s %@", __func__, [self commentInIndexPath:indexPath].content);
}
- (void)reportClick:(UIMenuController *)menu {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"%s %@", __func__, [self commentInIndexPath:indexPath].content);
}```