123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- //
- // 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
|