1. UIMenuController示例
2. UIMenuController须知
- 默认情况下,以下3个控件已经支持UIMenuController
- UITextView
- UITextFiled
- UIWebView
3. 让其他控件也支持UIMenuController(例:UILabel)
- 自定义UILabel
- 重写两个方法
//让label有资格成为第一响应者
- (BOOL)canBecomeFirstResponder {
return YES;
}
/**
* label能执行哪些操作(比如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;
}
- 实现剪切,复制,粘贴方法
//剪切
- (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;
}
- 让label成为第一响应者
//这里self是label
[self becomeFirstResponder];
- 显示UIMenuController
UIMenuController *menu = [UIMenuController sharedMenuController];
//targetRect: MenuController需要指向的矩形框
//targetView: targetRect会以targetView的左上角为坐标原点
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
4. 自定义UIMenuController内部的Item
//添加MenuItem(点击Item,默认会调用控制器的方法)
UIMenuItem *ding = [[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding:)];
UIMenuItem *replay = [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(replay:)];
UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)];
menu.menuItems = @[ding,replay,report];