Can I disable cut/copy in an iPhone application that displays some text via a label or what not? This data is purchased and I don't want them passing it around.
Really you should solve this some other way. What's stopping a user from simply writing down the text? But, for the sake of answering the question:
For a UITextView override the canBecomeFirstResponder function:
- (BOOL)canBecomeFirstResponder {
return NO;
}
And for a UITextField override canPerformAction:withSender:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(paste:))
return NO;
return [super canPerformAction:action withSender:sender];
}
Related
Hi,
I am developing an app in Xcode 4.3.2. I have a small question that is I want to enable scroll view property of UITextView and Disable the (copy,selectAll,define) properties of UITextView at a time in my code.
Is it possible?
Subclass UITextView and overwrite canBecomeFirstResponder:
- (BOOL)canBecomeFirstResponder {
return NO;
}
subclassing!
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
}
Subclassed UITextView
Here is h file
#interface CTextView : UITextView {
}
#end
Here is m file code
#import "CTextView.h"
#implementation CTextView
- (BOOL)canBecameFirstResponder {
return NO;
}
#end
Here is first UIViewController file in which subclassed UITextview is using
#import "First.h"
#import "CTextView.h"
textView = [[[CTextView alloc] initWithFrame:CGRectMake(0, 0, 320, 410)]autorelease];
[self.view addSubview:textView];
But still not able to prevent copy select all from UITextView. Please let me know if i am still missing anything or doing wrong.
Thanks for help.
Use this to disable copy:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
}
Got it. Now it is working
Here is the code for reference for anyone need it
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[UIMenuController sharedMenuController].menuVisible = NO; //do not display the menu
if (action == #selector(copy:))
{
return NO;
}
else if (action == #selector(selectAll:))
{
return NO;
}
[self resignFirstResponder]; //do not allow the user to selected anything
return NO;
return [super canPerformAction:action withSender:sender];
}
Now only problem having is Zooming. Now i have to work on that to disable it from UITextView.
Have you set the user interaction enabled to YES?
How do we disable Cut-Copy-Paste or Select-SelectAll menu when tapped on a UITextField. I tried with below code but it did not work.
if ([UIMenuController sharedMenuController]) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
Make a subclass if UITextView and implement this function
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(paste:) || action == #selector(copy:))//and put other actions also
return NO;
return [super canPerformAction:action withSender:sender];
}
canBecomeFirstResponderhere should do the trick. make sure you delegate your UITextField
- (BOOL)canBecomeFirstResponder {
return NO;
}
i want to restrict the copy or Paste option for particular UITextfield in my Application.
Add the following piece of code in the implementation file of the view controller which containing the UITextField
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
OR
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(paste:) // or #selector(copy:)
return NO;
return [super canPerformAction:action withSender:sender];
}
Create a subclass of UITextField.
In that subclass, implement
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (sel_isEqual(action, #selector(copy:))) //#selector(paste:) {
return NO;
}
return [super canPerformAction:action withSender:sender];
}
Then use this subclass for the field that you don't want to be able to copy in, and use a regular UITextField for the one that you can copy from.
Refer this URL for more info:-
iPhone – Disable the Cut/Copy/Paste Menu on UITextField
UIResponder
iPhone SDK Development
You can implement it like this:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(paste:)) {
return NO;
}
return [super canPerformAction:action withSender:sender];
}
Otherwise you can write:
- (BOOL)canBecomeFirstResponder {
return NO;
}
To make your UITextField non-editable.
I want to disable text selection on a UITextView. Until now what i've
already done is:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[UIMenuController sharedMenuController].menuVisible = NO;
if (action == #selector(paste:))
return NO;
if (action == #selector(select:))
return NO;
if (action == #selector(selectAll:))
return NO;
return NO;
}
In this away I set UIMenuController to hidden and i put a stop to text copy but the text selection is still visible.
Google results (also StackOverflow) take me to no solution. Has someone already faced the
same problem? Any ideas?
If you want to prevent the text selection but keep links interactions, add the following textview delegate methods
- (void)textViewDidChangeSelection:(UITextView *)textView
{
[textView setSelectedRange:NSMakeRange(NSNotFound, 0)];
}
If you want to disable cut/copy/paste on all UITextView of your application you can use a category with :
#implementation UITextView (DisableCopyPaste)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
#end
It saves a subclassing... :-)
Otherwise, just subclass UITextViewand put :
- (BOOL)canBecomeFirstResponder
{
return NO;
}
textView.editable = NO;
or
[textView setEnabled:NO];
im not sure what u meant