I have an iOS keyboard question.
Say we have a UITextField (let's call it textfield) and a standard virtual keyboard, with the Enter key set to Send. We'd like to disable the Send key when UITextField is empty, otherwise enable it. We have set enablesReturnKeyAutomatically to YES on the text field. This works fine when we manually edit the text in the text field.
However, the Send key doesn't seem to work fine when we programmatically change the text. Our app will clear the text field by calling textfield.text = #"" once the Send key is hit and the text is sent. In this case, we expect the Send key to be disabled since textfield's content is empty. But no -- it appears enabled.
We want to disable the Send key in such a case. Please share your experience if possible.
Thanks!
I think the best you can do is to ignore the return event this way:
- (BOOL) textFieldShouldReturn:(UITextField*) textField {
return textField.text.length > 0;
}
Related
I have a weird problem i am facing with. I know about OTP autofill with
textField.textContentType = .oneTimeCode .
But i have a different problem, i want this autofill to work but the text field should be disabled for any manual typing.
Basically , user gets message with OTP on his/her phone and OTP gets autofilled by using .oneTimeCode. But in any case if the app can't read or find OTP through this method(from SMS) , the user should NOT be allowed to manually enter the OTP.
Is there anyway to attain this scenario ?
You can set a transparent view on textField & make its userInteraction disable. & for showing keyboard automatically you can use textField.becomeFirstResponder()
You need to disable userInteraction of view that is on your TextField:
transparentView.isUserInteractionEnabled = false
And for display keyboard automatically:
Yoy can write this line in viewWillAppear method when your otpScreen appear.
textField.becomeFirstResponder()
At first glance, my suggestion will be adding clear view to block the text field touch, but this will prevent filling the OTP
Disabling user interaction will be preventing filling the OTP too.
Maybe you can use text field delegate on text change, check the new characters. When the characters are less than your OTP length then just ignore the new characters.
textField:shouldChangeCharactersInRange:replacementString:
I faced with an interesting problem. In my app, I have some UITextFields that could contain only one character inside.
From iPhone 6s (iOS 9) Apple has introduced a cool feature related with force touch on keyboard text field:
http://cdn.osxdaily.com/wp-content/uploads/2016/05/iphone-keyboard-trackpad.mov.gif
In my case when the text field can contain only one character is completely useless and can be very misunderstanding.
I went through the UITextField documentation and I haven't found anything that could help me.
Do you have any ideas how to solve this problem? Is it possible?
Thanks!
UPDATE
Related with #dmorrow answer:
Setting the isSecureTextEntry to true disables that feature, but in my case I want to see the input text. So the isSecureTextEntry must be false
I just tested, and textField.isSecureTextEntry = true disables this feature.
I'm not sure how you are switching your input from number to •, but if you just use the native password input you'll get the result you need.
New Idea
What if you used a hidden textfield for the input, with isSecureTextEntry = true. Each of your input fields would actually just be a UIButton. On tapping the button, the hidden textfield would become first responder. On text input, you could set the label of the button and jump to the next button as you are doing now. Any time the user taps the button, it would clear the input and allow them to type again.
Right now when the keyboard launches on my application it defaults to the letter side showing an alphabetical keyboard.
Question will be listed below images
Refer to image below:
This is good. Clicking 123 will show the number side.
The Question:
However, I want to by default show the number side and still be able to switch back to the letter side later WHEN THE KEYBOARD IS OPEN. How do I do this?
Setting the keyboard programmatically to a different type is NOT the answer!
For example: User clicks in the text field, keyboard pops up and defaults to let's say the ABC side. The user can click 123 to switch. This is what I want. If the ability to switch while the keyboard is open is taken away it defeats the point of this question.
So if the Name Phone Pad keyboard defaults to the ABC side initially. I want it to default to the 123 side that way while the keyboard is still open it can switch to ABC again when the user clicks ABC.
I want to do this because I have different settings for how to search for barcodes. Either by name of the product or number based on settings the user set about how to index results. This way if they are sorting by number it suggests numbers first, BUT they can still go back to searching by alphabetical if they wanted by clicking the ABC button on the keyboard.
Here is the current setting for my keyboard.
keyboardType is property for a UITextField.
textField.keyboardType = UIKeyboardType.UIKeyboardTypeNumberPad
and
textField.keyboardType = UIKeyboardType.UIKeyboardTypeDefault
is how you can switch between the two modes programatically. Hope that helps.
To switch the Keyboardlayout programmatically you have to mutate UITextfield's keyboardType attribute.
Try
textField.keyboardType = UIKeyboardType.NumberPad;
or
textField.keyboardType = UIKeyboardType.PhonePad;
I have an input field the user needs to fill with an alphanumeric code. The keyboard the user uses to type the code has a dynamic return button that changes to "send" as he writes some text on the field. When the field is empty the return button has the default value.
To dynamically change the return button type I use the following code:
if([textField.text isEqualToString:#""])
{
textField.returnKeyType = UIReturnKeyDefault;
}
else
{
textField.returnKeyType = UIReturnKeySend;
}
[textField reloadInputViews];
However this has the following drawback: since the code is alphanumeric, the user may be typing numbers, and yet the keyboard will always switch back to the letter keyboard, so to type more than one number in a row he will need to be continuously switching to number keyboard.
Is there any way to dynamically change the return key of a keyboard as the user types but to preserve the keyboard state to letters or numbers keyboard?
I think this is not a bug on Apple's side, more a missing implementation of API for the keyboard. With the new iOS8 API you might want to create your own keyboard returning the UIKeyboardType.
For iOS7 I worked around by inspecting the views of the keyboard. Use the US2KeyboardType CocoaPod or the source:
https://github.com/ustwo/US2KeyboardType
As Martin noted above, it's not a bug on Apple's side, but on my side. However I'll be posting the solution I've found since it is the one that solves that particular problem:
Instead of manually changing the return key type when there is text on the text field, Apple provides us with a property called enablesReturnKeyAutomatically that when set to YES it automatically manages the issue, enabling and disabling the return key depending on whether there is text or not in the text field.
Therefore you don't need to modify the returnKeyType property, and thus, no calling to reloadInputViews is required, so the keyboard doesn't change back to its original state.
In my app, I need to enable Return Key on UITextView even if UITextView is empty. By default, return key is disabled, before user not entered any symbols.
How I can do this?
Thanks.
I need to enable Return Key on UITextView even if UITextView is empty.
Actually, the return key is enabled by default no matter the text view is empty or not. However, if you want to set the return key to disabled by default, you need to set the enablesReturnKeyAutomatically to YES. As the Apple Doc said:
The default value for this property is NO. If you set it to YES, the keyboard disables the return key when the text entry area contains no text. As soon as the user enters any text, the return key is automatically enabled.