UIMenuController即菜单控制器,是一个单例对象,用来复制,粘贴,删除等内容的操作。
使用方法,显示默认弹框:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:CGRectMake(100, 300, 200, 20) inView:self.view];
menu.menuVisible = YES;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
支持的操作类型
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
{
return YES;
} else if (action == @selector(copy:))
{
return YES;
}
else{
return NO;
}
}
如果想自定义弹出框内容,然后实现对应的方法即可。
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:CGRectMake(100, 300, 200, 20) inView:self.view];
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.menuVisible = YES;
这里刚开始发现默认弹出的剪贴框是英文的,如果想显示为中文,执行以下操作:
在plist里面Localization native development region 选择 china ,然后Localized resources can be mixed 选 YES,如果没有Localized resources can be mixed,需手动添加