ios - UILabel_长按复制
Posted 神来芒果
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios - UILabel_长按复制相关的知识,希望对你有一定的参考价值。
1.添加长按的手势
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenuAction:)];
[self.lable setUserInteractionEnabled:YES]; [self.lable addGestureRecognizer:longGesture];
2.实现方法
- (BOOL)canBecomeFirstResponder{ return YES; } - (BOOL)canPerformAction:(SEL)selector withSender:(id) sender { if (selector == @selector(copy:)) { return YES; } return NO; } - (void)copy:(id)sender { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:self.lable.text]; } - (void)showMenuAction:(UILongPressGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateBegan) { [self becomeFirstResponder]; CGPoint location = [sender locationInView:[sender view]]; UIMenuController *menuController = [UIMenuController sharedMenuController]; [menuController setTargetRect:CGRectMake(location.x, location.y, 0.0f, 0.0f) inView:sender.view]; [menuController setMenuVisible:YES animated:YES]; } }
以上是关于ios - UILabel_长按复制的主要内容,如果未能解决你的问题,请参考以下文章
如何通过长按制作textField或UILabel,使其可以“复制”,但不能编辑
Clipboard 自动复制功能,ios复制失败,换方案 user-select: text ;长按复制 (ios 兼容,长按复制)