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.
Related
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];
}
I am making a rich text editor app. When app started, UIWebView loads a html file which contains a content editable div.
When I touch a word, UIWebView shows a menu suggest some words instead of selected word. How can I turn off this menu?
That's what I say:
http://i.stack.imgur.com/Xu4A9.png
This is my code:
- (void)viewDidLoad
{
webText=nil;
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
[self.editor loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
self.editor.delegate=self;
[super viewDidLoad];
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(defineSelection:))
{
return NO;
}
else if (action == #selector(translateSelection:))
{
return NO;
}
else if (action == #selector(copy:))
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
Here you have to return NO for all the specific methods the menu controller performs.
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(defineSelection:))
{
return NO;
}
else if (action == #selector(translateSelection:))
{
return NO;
}
else if (action == #selector(copy:))
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
Hope this helps .
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 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