最近有个需求是要仿照微信朋友圈中长按文字弹出复制的功能,总结如下
参考文章: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;
}