@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];
}
网友评论