iOS开发-UILabel实现长按复制等功能

最近有个需求是要仿照微信朋友圈中长按文字弹出复制的功能,总结如下

参考文章:https://blog.csdn.net/u011347072/article/details/49506977

第一种方法

@interface CopyLable0 : UILabel

@end


@interface CopyLable0 ()

@property (nonatomic, strong) UIPasteboard *pasteboard;

@end

@implementation CopyLable0

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.numberOfLines = 0;
        self.pasteboard = [UIPasteboard generalPasteboard];
        [self attachLongTapHandle];
    }
    return self;
}


- (void)attachLongTapHandle {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [UILongPressGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
        // 防止长按之后连续触发该事件
        if (sender.state == UIGestureRecognizerStateBegan) {
            // UILabel成为第一响应者
            [self becomeFirstResponder]; 
            UIMenuController *menuVC = [UIMenuController sharedMenuController];
            [menuVC setTargetRect:self.frame inView:self.superview];
            [menuVC setMenuVisible:YES animated:YES];
        }
    }];
    [self addGestureRecognizer:longPress];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copy:)) {
        return YES;
    }
    if (action == @selector(paste:)) {
        return YES;
    }
    if (action == @selector(delete:)) {
        return YES;
    }
    if (action == @selector(selectAll:)) {
        return YES;
    }
    if (action == @selector(cut:)) {
        return YES;
    }
    return NO;
}

- (void)copy:(id)sender {
     NSLog(@"copy");
     self.pasteboard.string = self.text;
}

- (void)paste:(id)sender {
     NSLog(@"paste");
}

- (void)delete:(id)sender {
   NSLog(@"delete");
}

- (void)selectAll:(id)sender {
    NSLog(@"selectAll");
}

- (void)cut:(id)sender {
    NSLog(@"cut");
}
@end

第二种方法

@interface CopyLabel1 : UILabel

@end

@interface CopyLabel1 ()

@property (nonatomic, strong) UIPasteboard *pasteBoard;

@end

@implementation CopyLabel1

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.numberOfLines = 0;
        [self attachLongPress];
        self.pasteBoard = [UIPasteboard generalPasteboard];
    }
    return self;
}

- (void)attachLongPress{
    self.userInteractionEnabled = YES;
    
    UILongPressGestureRecognizer *longPress = [UILongPressGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
         // 防止长按之后连续触发该事件
        if (sender.state == UIGestureRecognizerStateBegan) {
            [self becomeFirstResponder];
            UIMenuItem *copyMenuItem = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyAction:)];
            UIMenuItem *pasteMenueItem = [[UIMenuItem alloc]initWithTitle:@"粘贴" action:@selector(pasteAction:)];
            UIMenuItem *cutMenuItem = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(cutAction:)];
            UIMenuController *menuController = [UIMenuController sharedMenuController];
            [menuController setMenuItems:[NSArray arrayWithObjects:copyMenuItem, pasteMenueItem,cutMenuItem, nil]];
            [menuController setTargetRect:self.frame inView:self.superview];
            [menuController setMenuVisible:YES animated:YES];
        }

    }];
    [self addGestureRecognizer:longPress];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copyAction:)) {
        return YES;
    }
    if (action == @selector(pasteAction:)) {
        return YES;
    }
    if (action == @selector(cutAction:)) {
        return YES;
    }
    return NO; //隐藏系统默认的菜单项
}

- (void)copyAction:(id)sender {
    self.pasteBoard.string = self.text;
    NSLog(@"粘贴的内容为%@", self.pasteBoard.string);
}

- (void)pasteAction:(id)sender {
    self.text = self.pasteBoard.string;
}

- (void)cutAction:(id)sender  {
    self.pasteBoard.string = self.text;
    self.text = nil;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,019评论 3 119
  • 曾经无数次憧憬过初恋是什么样?是怎样的甜美、如何的浪漫,最后怎么就成为美好的回忆了?只不过没有想到过,曾经期望美好...
    苍松月影阅读 283评论 4 1
  • 三、开场:怎样构建一个话题 在这一讲中,我会告诉你,怎样打破沉默,说出第一句话。怎样寻找合适的话题与对方聊,怎样让...
    传说的烟火阅读 434评论 0 2
  • 线条学了大概一个月,正式临摹的一幅。还在淡墨+花青的初次分染阶段
    旭旭的太阳集阅读 421评论 0 2