Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/enable the return key in a UITextField?

Is there a way to programmatically enable or disable the Return Key on the UIKeyboard? The closest I could find is enablesReturnKeyAutomatically, but that only will tell whether to disable it at all.

like image 992
Sophie Alpert Avatar asked Apr 25 '09 06:04

Sophie Alpert


People also ask

How do I activate the return button on my Iphone?

Tap “123” key in the bottom-left corner of the keyboard. 2. The Return key is located in the bottom-right corner, next to the space bar.


2 Answers

Maybe the following code segment helps:

textfield.enablesReturnKeyAutomatically = YES; 

This is publicly available in iPhone SDK in UITextInputTraits. Using this, return key will be disabled when no input text is available within text field.

like image 174
Tharindu Madushanka Avatar answered Sep 22 '22 02:09

Tharindu Madushanka


You can override UITextField's hasText attribute to achieve this:

class CustomTextField : UITextField {     override public var hasText: Bool {         get {             return evaluateString(text)         }     } } 

Where evaluateString(_ text: String?) -> Bool checks against your needed input criteria, for example character count.

Of course this does only work in combination with enablesReturnKeyAutomatically = true set on the UITextField.

I am aware that my answer is neither timely nor written in Objective-C, but given that I have not been able to find an answer anywhere else and this question being routinely referred to in other threads, I think that here is the best place to post it.

like image 29
Janne Avatar answered Sep 22 '22 02:09

Janne