思路来源http://stackoverflow.com/questions/1920541/enable-copy-and-paste-on-uitextfield-without-making-it-editable
首先从UITextView继承一个子类并重写以下方法:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:)) {
return YES;
}else{
return [super canPerformAction:action withSender:sender];
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)becomeFirstResponder
{
if ([super becomeFirstResponder]) {
return YES;
}
return NO;
}
- (void)paste:(id)sender
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = board.string;
[self resignFirstResponder];
}
然后在实例化时将textView的eidable属性设为NO即可;