- (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);
// 比如我在一个 UITextView 里,想增加全选和复制的方法
// 只要在自定义 UITextView 的时候加入这行代码即可
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(selectAll:) || action == @selector(copy:)) return YES;
return NO;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)setUp {
/* 你可以在这里添加一些代码,比如字体、居中、夜间模式等 */
self.userInteractionEnabled = YES;
[self addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)]];
}
- (void)longPress {
// 设置label为第一响应者
[self becomeFirstResponder];
// 自定义 UIMenuController
UIMenuController * menu = [UIMenuController sharedMenuController];
UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyText:)];
menu.menuItems = @[item1];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(copyText:)) return YES;
return NO;
}
// 如果模仿上面的写以下代码,点击后会导致程序崩溃
if(action == @selector(selectAll:) || action == @selector(copy:)) return YES;
- (void)copyText:(UIMenuController *)menu {
// 没有文字时结束方法
if (!self.text) return;
// 复制文字到剪切板
UIPasteboard * paste = [UIPasteboard generalPasteboard];
paste.string = self.text;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有