// // CCCopyLabel.m // YouHuiProject // // Created by 小花 on 2018/7/11. // Copyright © 2018年 kuxuan. All rights reserved. // #import "CCCopyLabel.h" @implementation CCCopyLabel -(BOOL)canBecomeFirstResponder { return YES; } // 可以响应的方法 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(newFunc)) { return YES; } return NO; } //针对于响应方法的实现 -(void)copy:(id)sender { UIPasteboard *pboard = [UIPasteboard generalPasteboard]; if (self.textStr.length > 0) { pboard.string = self.textStr; }else { pboard.string = self.text; } } //UILabel默认是不接收事件的,我们需要自己添加touch事件 -(void)attachTapHandler { self.userInteractionEnabled = YES; UILongPressGestureRecognizer *touch = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [self addGestureRecognizer:touch]; } //绑定事件 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self attachTapHandler]; [[NSNotificationCenter defaultCenter] addObserverForName:UIMenuControllerWillHideMenuNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { self.backgroundColor = [UIColor whiteColor]; }]; } return self; } -(void)awakeFromNib { [super awakeFromNib]; [self attachTapHandler]; } -(void)handleTap:(UIGestureRecognizer*) recognizer { if (recognizer.state == UIGestureRecognizerStateEnded) { return; }else if (recognizer.state == UIGestureRecognizerStateBegan){ [self becomeFirstResponder]; // self.backgroundColor = UIColorRGB(236, 236, 236, 1.0); self.backgroundColor = [UIColor colorWithRed:236/255.f green:236/255. blue:236/255. alpha:1.f]; UIMenuItem * item = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(newFunc)]; [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview]; [UIMenuController sharedMenuController].menuItems = @[item]; [UIMenuController sharedMenuController].menuVisible = YES; } } -(void)newFunc{ UIPasteboard *pboard = [UIPasteboard generalPasteboard]; if (self.textStr.length > 0) { pboard.string = self.textStr; }else { pboard.string = self.text; } } @end