UILabel 的復制

UILabel默認是不響應Touch事件的,也無法復制,那么我們就需要自己實現(xiàn)一個可復制的。
以實現(xiàn)可復制的UILabel為例:
方法一:
實現(xiàn)步驟:
1、自定義一個類繼承自UILabel
2、讓label可以接收到事件,也就是成為第一響應者。即實現(xiàn)-(BOOL)canBecomeFirstResponder;
3、實現(xiàn)-(BOOL)canPerformAction:(SEL)action withSender:(id)sender,放出你需要的功能,比如你要放出copy,你就返回YES,否則返回NO;
4、給label添加touch事件,label默認是不接收事件的??梢蕴砑娱L按手勢等
5、創(chuàng)建彈出菜單UIMenuItem、UIMenuController,并將UIMenuItem添加到UIMenuController上。
6、實現(xiàn)UIMenuItem的相關方法,在相關方法中創(chuàng)建剪切板UIPasteboard(可直接使用系統(tǒng)的,也可自定義)。
7、至此完成收工。
附上代碼:

//為了能接收到事件(能成為第一響應者)
-(BOOL)canBecomeFirstResponder {
    return YES;
}

// 可以響應的方法
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:));
}

//針對于響應方法的實現(xiàn)
-(void)copy:(id)sender {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    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];
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    [self attachTapHandler];
}

-(void)handleTap:(UIGestureRecognizer*) recognizer {
    [self becomeFirstResponder];
     UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"復制"
                                                      action:@selector(copy:)];
    CGPoint location = [recognizer locationInView:[recognizer view]];
    CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
    
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    [[UIMenuController sharedMenuController] setTargetRect:menuLocation inView:[recognizer view]];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender;包含的功能有:copy,paste,cut,select,selectAll,delete等,需要實現(xiàn)什么方法在此方法中返回即可。如下

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:) || action ==@selector(paste:) || action ==@selector(cut:)  || action ==@selector(select:) || action ==@selector(delete:));
}

方法二:
如果label只是實現(xiàn)一個復制功能,我們也可以直接使用UITextView,創(chuàng)建一個對象,禁止它編輯,即[TextView setEditable:NO];即可。如果想過濾一下彈框的選項,那就自定義,重新實現(xiàn)-(BOOL)canPerformAction:(SEL)action withSender:(id)sender;好嘍。
實現(xiàn)步驟:
1、自定義一個類繼承自UITextView
2、將UITextView設置為不可編輯:[self setEditable:NO];
3、直接實現(xiàn):-(BOOL)canPerformAction:(SEL)action withSender:(id)sender;去掉不需要的

//* 選中文字后的菜單響應的選項 */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(copy:) || action == @selector(selectAll:)) { // 菜單不能響應copy項或菜單不能響應select all項
return YES;
}
return NO;
}

4、至此完成收工。是不是更簡單
備注:因為UITextView直接就可以稱為第一響應者,所以不需要實現(xiàn)- (BOOL)canBecameFirstResponder;方法

PS:希望對您有點幫助,也可以加我QQ好友(1224740397),很樂意與您分享,共同學習。

Life is a sail trip full of chances and challenges.
人生的航行充滿了機遇與挑戰(zhàn)。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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