美文网首页
iOS UILabel 复制按钮大坑

iOS UILabel 复制按钮大坑

作者: yimiao | 来源:发表于2020-06-29 17:04 被阅读0次
    @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];
    }
    
    

    相关文章

      网友评论

          本文标题:iOS UILabel 复制按钮大坑

          本文链接:https://www.haomeiwen.com/subject/kdqafktx.html