码迷,mamicode.com
首页 > 移动开发 > 详细

ios - UILabel_长按复制

时间:2018-07-07 17:30:05      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:stat   visible   shared   div   can   ges   name   ati   first   

 

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_长按复制

标签:stat   visible   shared   div   can   ges   name   ati   first   

原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/9277289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!