- (void)testMenuClicke:(UIButton *)btn{
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:btn.frame inView:btn.superview];
// [UIMenuController sharedMenuController].arrowDirection = UIMenuControllerArrowRight;
UIMenuItem * item = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copys:)];
UIMenuItem * item1 = [[UIMenuItem alloc] initWithTitle:@"粘贴" action:@selector(pastes:)];
UIMenuItem * item2 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deletes:)];
[menu setMenuItems:@[item, item1,item2]];
[menu setMenuVisible:YES animated:YES];
//[[UIMenuController sharedMenuController] update];
//menu.arrowDirection = UIMenuControllerArrowLeft;
}
// 不设置这个方法,会导致无法调出menu菜单
- (BOOL)canBecomeFirstResponder {
return true;
}
// 再此方法中判断允许的方法,否则菜单项不弹出
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
// UIResponder
if(action == @selector(copys:) ||
action == @selector(pastes:) ||
action == @selector(deletes:)) {
return YES;
}
return NO;
}
// 实现方法,完成功能
- (void)copys:(id)sender {
// UIPasteboard * paste = [UIPasteboard generalPasteboard];
// paste.string = self.text;
NSLog(@"copy");
}
- (void)pastes:(id)sender {
// UIPasteboard * paste = [UIPasteboard generalPasteboard];
// self.text = paste.string;
NSLog(@"paste");
}
- (void)deletes:(id)sender {
// self.text = @"";
NSLog(@"delete");
}
第二中
- (void)testMenuClicke:(UIButton *)btn{
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:btn.frame inView:btn.superview];
// // [UIMenuController sharedMenuController].arrowDirection = UIMenuControllerArrowRight;
// UIMenuItem * item = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copys:)];
// UIMenuItem * item1 = [[UIMenuItem alloc] initWithTitle:@"粘贴" action:@selector(pastes:)];
// UIMenuItem * item2 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deletes:)];
//
// [menu setMenuItems:@[item, item1,item2]];
[menu setMenuVisible:YES animated:YES];
//[[UIMenuController sharedMenuController] update];
//menu.arrowDirection = UIMenuControllerArrowLeft;
}
// 不设置这个方法,会导致无法调出menu菜单
- (BOOL)canBecomeFirstResponder {
return true;
}
// 再此方法中判断允许的方法,否则菜单项不弹出
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
// UIResponder
if(action == @selector(copy:) ||
action == @selector(paste:) ||
action == @selector(delete:) )
{
return YES;
}
return NO;
}
// 实现方法,完成功能
- (void)copy:(id)sender {
// UIPasteboard * paste = [UIPasteboard generalPasteboard];
// paste.string = self.text;
NSLog(@"copy");
}
- (void)paste:(id)sender {
// UIPasteboard * paste = [UIPasteboard generalPasteboard];
// self.text = paste.string;
NSLog(@"paste");
}
- (void)delete:(id)sender {
// self.text = @"";
NSLog(@"delete");
}
- (void)cut:(id)sender {
// UIPasteboard * paste = [UIPasteboard generalPasteboard];
// paste.string = self.text;
// self.text = @"";
NSLog(@"cut");
}
-
如下:监听menu即将显示的通知
- 1.注册通知监听
// 注册监听 菜单即将显示 通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show:) name:UIMenuControllerWillShowMenuNotification object:nil];
- 2.实现监听到menu菜单显示调用方法
// 指定 menu 弹出菜单显示什么 内容 ,默认 NO 都不显示
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
return NO;
}
- 3.dealloc方法中,移除通知监听
- 1.注册通知监听
- (void)dealloc{
// 移除监听通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//- (void)cut:(nullable id)sender NS_AVAILABLE_IOS(3_0);//剪切
//- (void)copy:(nullable id)sender NS_AVAILABLE_IOS(3_0);//复制
//- (void)paste:(nullable id)sender NS_AVAILABLE_IOS(3_0);//粘贴
//- (void)select:(nullable id)sender NS_AVAILABLE_IOS(3_0);//选择
//- (void)selectAll:(nullable id)sender NS_AVAILABLE_IOS(3_0);//全选
//- (void)delete:(nullable id)sender NS_AVAILABLE_IOS(3_2);//删除
//- (void)makeTextWritingDirectionLeftToRight:(nullable id)sender NS_AVAILABLE_IOS(5_0);//左对齐
//- (void)makeTextWritingDirectionRightToLeft:(nullable id)sender NS_AVAILABLE_IOS(5_0);//右对齐
//- (void)toggleBoldface:(nullable id)sender NS_AVAILABLE_IOS(6_0);//粗体
//- (void)toggleItalics:(nullable id)sender NS_AVAILABLE_IOS(6_0);//斜体
//- (void)toggleUnderline:(nullable id)sender NS_AVAILABLE_IOS(6_0);//下划线
//
//- (void)increaseSize:(nullable id)sender NS_AVAILABLE_IOS(7_0);//字号增大
//- (void)decreaseSize:(nullable id)sender NS_AVAILABLE_IOS(7_0);//字号减小
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //当menucontroller显示,点击不同的cell时为什么会显示。 // menuController的显示依赖于第一响应者,当点击另外的cell时,当前cell取消第一响应者状态,menucontroller自动消失 UIMenuController * menu = [UIMenuController sharedMenuController]; NSLog(@"%d",menu.isMenuVisible); //防止点击多次创建 if (menu.isMenuVisible) { [menu setMenuVisible:NO animated:YES]; } else {
ZZYTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; self.selectCell = cell;
[cell becomeFirstResponder];
UIMenuItem * item0 = [[UIMenuItem alloc]initWithTitle:@"分享" action:@selector(share:)]; UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"评论" action:@selector(comment:)]; UIMenuItem * item2 = [[UIMenuItem alloc]initWithTitle:@"点赞" action:@selector(praise:)]; menu.menuItems = @[item0,item1,item2];
[menu setTargetRect:CGRectMake(0, cell.frame.size.height * 0.5, cell.frame.size.width, cell.frame.size.height) inView:cell];
[menu setMenuVisible:YES animated:YES]; } }
- (void)share:(UIMenuController *)menu { NSLog(@"%@",self.selectCell.textLabel.text);
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:self.selectCell.textLabel.text delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show];
}
- (void)comment:(UIMenuController *)menu {
}
- (void)praise:(UIMenuController *)menu {
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { UIMenuController * menu = [UIMenuController sharedMenuController]; [menu setMenuVisible:NO animated:YES]; } |