Disable cursor and copy / paste in UITextView (fast)

I have a UITextField for which I use UIPickerView as its inputView . The user can select a value from the collector, and the selected value is populated in my TextInput .
This circuit works fine, but with the following problems:

1) I want to disable the cursor, which is still displayed in a UITextField .
I tried disabling the UITextField , but then it does not respond to touches, and then the UIPickerView does not show - which makes it useless.
2) Since the picker is shown and the keyboard is not vein, the user clicks on the text field, the user cannot enter anything, but still can paste the text copied from another, where long press. How can I disable this?

I can not find relevant information on the Internet. Should I use Label or Button for this instead of UITextInput ?

+7
ios uitextfield xcode swift uipickerview
source share
4 answers

I think an easy way is to do this using the UITextField button

Sorry - this is not for Swift for Obj-C , but this is an idea:

To do what you want with UITextField , you must subclass UITextField and try to use this code to disable / hide carriage and input (copy / paste)

 - (CGRect) caretRectForPosition:(UITextPosition*) position { return CGRectZero; } - (NSArray *)selectionRectsForRange:(UITextRange *)range { return nil; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:)) { returnNO; } return [super canPerformAction:action withSender:sender]; } 

An example from here: http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/

Anyway ... this is a "functional" example: https://github.com/hackiftekhar/IQDropDownTextField

+6
source share

As TonyMkenu said, you need to subclass UITextField and then implement the methods that it has above. Here's a Swift for those of you who don't know Objective-C:

 override func caretRectForPosition(position: UITextPosition!) -> CGRect { return CGRect.zeroRect } override func selectionRectsForRange(range: UITextRange) -> [AnyObject] { return [] } override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool { // Disable copy, select all, paste if action == Selector("copy:") || action == Selector("selectAll:") || action == Selector("paste:") { return false } // Default return super.canPerformAction(action, withSender: sender) } 
+7
source share

Totally silly to hack, but if you set the hue color of the text field in the UIView section of the Interface Builder property inspector to match the background color, the cursor will be invisible:

uz5Tt.png

+1
source share

A UITextField delegate can implement the textFieldShouldBeginEditing method. If this method always returns NO , then the cursor will never appear and a long press will not be allowed.

When calling the method, you can display the UIPickerView . However, UIPickerView cannot be an inputView . He must be the child of the standard UIView that you live from below. Or you can simply use the hidden property for the UIView to hide / show the view as needed.

0
source share

All Articles