Disabling menu on tap of UITextField - iphone

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;
}

Related

iOS disable cut/copy in iPhone

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];
}

How to remove Copy,Select All,Define menuItem from UIMenuController

As my this question Display i want to display pop up when user select the text. and in that pop up detail about that word will be displayed.
But i didn't get any satisfactory answer so i have change my logic.
Now i want to display one item like Pop-Up in my UIMenuController and when user click that option than pop-up will displayed.
I have achieved this using this code,
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Pop-Up" action:#selector(displayPopUp:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
So my option is displaying and when i click that option than pop-up displays.But some other option is also display which i don't wanna, like this
I have googled it and get this code
- (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];
}
But it didn't remove this extra item in UIMenuController.
The canPerformAction method is sent to everyone in the Responder chain. So, if the code you mention above is in the ViewController but the UITextView is the first Responder, it will not work. I found that the easiest thing to do was subclass UITextView and put the canPerformAction code in there. I disable all the default menuItems and create a menu of my own.
class rtfView: UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any!) -> Bool {
if (action == #selector(textItem(_:))) || (action == #selector(h1Item(_:))) || (action == #selector(h2Item(_:))) || (action == #selector(h3Item(_:))) {
return true
} else {
return false
}
}
}

Hide UIMenuController in UITextView

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 to restrict Copy, Paste for UITextField in iPhone?

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.

Disable text selection UITextView

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