views:

1201

answers:

2
  1. Can we enable the cut copy paste menu for a UILabel as it is for a UITextField?

  2. If not, and I need to convert my UILabel to UITextField, how can I enable the cut copy paste menu and not allow the content to be modified?

+2  A: 

Override the UITextField instance's textFieldShouldBeginEditing method, and set it to return NO in order to disable editing.

Take a look at the UITextFieldDelegate protocol for more details.

Alex Reynolds
The problem is: copy and paste won't work if you disable editing.
mrueg
+1  A: 

I got the copy & paste menu working on a UILabel, I just had to return YES for canBecomeFirstResponder and later call [label becomeFirstResponder] when the said label was to come on screen. As for returning YES from canBecomeFirstResponder, you can create a custom subclass or patch UILabel using a category:

@implementation UILabel (Clipboard)

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

@end

The category solution feels a bit hackish, but if you know what you’re doing it might be easier than subclassing.

zoul