UITextFieldui
UITextViewcode
UIWebVieworm
...开发
// 一、让自定义的 UILabel 有资格成为第一响应者 - (BOOL)canBecomeFirstResponder { return YES; } // 二、自定义的 UILabel 的 Menu 能执行哪些操做 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { // cut:剪切 / copy:复制 / paste:粘贴 if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) { return YES; } return NO; } // 三、那这些方法怎么实现呢 - (void)cut:(UIMenuController *)menu { // 先将本身的文字复制到粘贴板 [self copy:menu]; // 在清空文字 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; } // 四、系统的 menuController 方法还有 selector: / selectAll: /delete: 等
- (void)labelClick { // 让 自定义的label 成为第一响应者 [self.label becomeFirstResponder]; // 建立 UIMenuController UIMenuController *menu = [UIMenuController sharedMenuController]; // 设置 UIMenuController 的显示区域,targetRect 为指向的矩形框,inView 为以该 view 的左上角为原点 [menu setTargetRect:self.view.bounds inView:self.view]; // 设置可见 [menu setMenuVisible:YES animated:YES]; }
// 一、让自定义的 UILabel 有资格成为第一响应者 - (BOOL)canBecomeFirstResponder { return YES; } // 二、自定义的 UILabel 的 Menu 能执行哪些操做 , 自定义返回 NO - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { return NO; }
外部控制器里, 给 label 添加点击手势,在手势方法里get
- (void)labelClick { UIMenuController *menu = [UIMenuController sharedMenuController]; // 先处理以前的 if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } else { [self.label becomeFirstResponder]; // 建立自定义的 UIMenuItem UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"zhang1" action:@selector(item1Click)]; UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"zhang2" action:@selector(item2Click)]; // 设置 MenuItem [menu setMenuItems:@[item1, item2]]; [menu setTargetRect:self.view.bounds inView:self.view]; [menu setMenuVisible:YES animated:YES]; } } // 实现 UIMenuItem 对应的方法 - (void)item1Click { } - (void)item2Click { }
// 一、让自定义的 cell 有资格成为第一响应者 - (BOOL)canBecomeFirstResponder { return YES; } // 二、自定义的 cell 的 Menu 能执行哪些操做, 自定义返回 NO - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { return NO; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIMenuController *menu = [UIMenuController sharedMenuController]; // 先处理以前的 if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } else { DIYCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell becomeFirstResponder]; UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"zhang1" action:@selector(item1Click)]; UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"zhang2" action:@selector(item2Click)]; [menu setMenuItems:@[item1, item2]]; // 设置 menu 的指向的矩形框 CGRect rect = CGRectMake(0, cell.diy_height * 0.5, cell.diy_width, cell.diy_height * 0.5); [menu setTargetRect:rect inView:cell]; [menu setMenuVisible:YES animated:YES]; } }