iOS UILabel 复制按钮大坑

@interface UICopyLabel:UILabel
//非常关键的大坑!!!!!!!!
//FirstResponder必须要声明inputView
//不然就会往nextResponser寻找inputView属性
//找到后当作键盘使用
//当时我在controller里声明了一个自己写的inputView
//结果长按后那个按钮布局就莫名其妙地变了
//调试后发现被当成了键盘使用
@property (nonatomic,strong) UIView *inputView; 
@end

@implementation UICopyLabel

-(BOOL)canBecomeFirstResponder{
    return YES;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:));
}

-(void)copy:(id)sender{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.text;
}

@end


//长按手势
-(void)longPressAction:(id)sender{
    [self.lblText becomeFirstResponder];
    [[UIMenuController sharedMenuController] setTargetRect:self.lblText.frame inView:self.vlblTextBg];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。