iOS開發(fā)-UILabel實(shí)現(xiàn)長按復(fù)制等功能

最近有個需求是要仿照微信朋友圈中長按文字彈出復(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,203評論 3 119
  • 曾經(jīng)無數(shù)次憧憬過初戀是什么樣?是怎樣的甜美、如何的浪漫,最后怎么就成為美好的回憶了?只不過沒有想到過,曾經(jīng)期望美好...
    蒼松月影閱讀 308評論 4 1
  • 三、開場:怎樣構(gòu)建一個話題 在這一講中,我會告訴你,怎樣打破沉默,說出第一句話。怎樣尋找合適的話題與對方聊,怎樣讓...
    傳說的煙火閱讀 528評論 0 2
  • 線條學(xué)了大概一個月,正式臨摹的一幅。還在淡墨+花青的初次分染階段
    旭旭的太陽集閱讀 439評論 0 2

友情鏈接更多精彩內(nèi)容