最近有個需求是要仿照微信朋友圈中長按文字彈出復(fù)制的功能,總結(jié)如下
參考文章: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) {
// 防止長按之后連續(xù)觸發(fā)該事件
if (sender.state == UIGestureRecognizerStateBegan) {
// UILabel成為第一響應(yīng)者
[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) {
// 防止長按之后連續(xù)觸發(fā)該事件
if (sender.state == UIGestureRecognizerStateBegan) {
[self becomeFirstResponder];
UIMenuItem *copyMenuItem = [[UIMenuItem alloc]initWithTitle:@"復(fù)制" 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; //隱藏系統(tǒng)默認(rèn)的菜單項(xiàng)
}
- (void)copyAction:(id)sender {
self.pasteBoard.string = self.text;
NSLog(@"粘貼的內(nèi)容為%@", self.pasteBoard.string);
}
- (void)pasteAction:(id)sender {
self.text = self.pasteBoard.string;
}
- (void)cutAction:(id)sender {
self.pasteBoard.string = self.text;
self.text = nil;
}