Disable text selection UITextView - iphone

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

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

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?

UItextField Delegate not working

In my iPad app I have two textfields. One displays the normal, default textfield and the other should display a picker as its input view.
Problem is, once I use the txt1 which displays default keyboard and then when I touch the 2nd textField the txt1 keyboard is staying visible.
I have also written [txt1 resignFirstResponder]; [txt2 resignFirstResponder]; while displaying the picker.
I have checked the txt1 IBOutlet connection and the delegate assignment, those seem to be correct.
What am I missing?
Write the following code :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txt1)
{
return YES;
}
else
{
return NO; // Write the code for displaying UIPickerView instead of the Keyboard.
}
}
Hope this might solve your issue......
txt2.userInteractionEnabled = NO;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txt1)
{
[txt2 resignFirstResponder];
// code for Hide Picker
return YES;
   }
else {
// [txt2 resignFirstResponder];
[txt1 resignFirstResponder];
// code for go in picker
return YES;
}
}
for more information
You have to implement below method to resign Keyboard......
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Have u implemented this method ??
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
In your viewDidLoad method write this,
txt2.inputView = pickerView;
and other resignFirstResponder codes should be placed correctly , by this on Tapping txt2, you will directly get pickerview instead of Keyboard.
did you implement the delegates property of UITextFieldDelegates to in the header file, if not do that and check

Disabling menu on tap of UITextField

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

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.