创建一个继承UITextView的textView
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(copy:) ||
action ==@selector(selectAll:)||
action ==@selector(cut:)||
action ==@selector(select:)||
action ==@selector(paste:)) {
return YES;//
}
returnNO;
}
禁止长按弹出UIMenuController菜单,但是这样做的话,想要复制UITextField内容的操作也一起被禁止了:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
其中下边这些是可以访问的公开方法
- (void)cut:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)copy:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)paste:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)select:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)selectAll:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)delete:(nullableid)senderNS_AVAILABLE_IOS(3_2);//该方法没有实现,直接不响应好了,或者实现下方法
- (void)makeTextWritingDirectionLeftToRight:(nullableid)senderNS_AVAILABLE_IOS(5_0);
- (void)makeTextWritingDirectionRightToLeft:(nullableid)senderNS_AVAILABLE_IOS(5_0);
实现部分方法:
- (void)copy:(UIMenuController *)menu
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = self.text;
}
- (void)paste:(UIMenuController *)menu
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = board.string;
} - (void)cut:(UIMenuController *)menu
{
[self copy:menu];
self.text = nil;
} - (BOOL)canBecomeFirstResponder
{
return YES;
}
也可以自定义自己的MenuController
//返回YES,会调起菜单 内部可以自定义菜单UIMenuItem
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
UIMenuController *menuControl = [UIMenuController sharedMenuController];
UIMenuItem *item = [[UIMenuItem alloc]initWithTitle:@"插入表情" action:@selector(insertFace:)];
menuControl.menuItems = @[item];
//保留系统的拷贝和粘贴菜单
if (action==@selector(insertFace:) || action == @selector(copy:) || action == @selector(paste:)) {
return YES;
}
return NO;
}
- (void)insertFace:(UIMenuController *)menu
{
NSLog(@"插入表情");
} - (void)copy:(UIMenuController *)menu
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = self.text;
}
(void)paste:(UIMenuController *)menu
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = board.string;
}(BOOL)canBecomeFirstResponder
{
return YES;
}
@end