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文章

iOS IBInspectable和IB_DESIGNABLE

2020-2-22 11:44:42

iOS文章

iOS UIImage使用

2020-2-22 12:55:29

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索