Using Storyboard, I setup a UITextField and two static UILabels. I setup the constraints so the static labels spread out as user types in the TextField.
[Label1] [TextField] [Label2]
[Label1] [TextFieldIsGettingFilled] [Label2]
Everything is fine until now. The TextField is getting wider as user types in. However, if the user uses backspace (deletes character), TextField doesn't get narrower. So it becomes like:
[Label1] [TextField ] [Label2]
What is the way of detecting backspace and narrow TextFields' width accordingly while user is still typing?
EDIT
I made an example and this works (increases and decreases according to text):
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.invalidateIntrinsicContentSize()
return true
}
Source: Resize a UITextField while typing (by using Autolayout)
Related
I found something similar to my problem checkout here but it does not work for me: link
Thid UITextView delegate method calls twice If we tap on predictive text
1st call insert text 2nd call insert space after if
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
I found one hack:
if text.count > 1 { return false }
But this logic failed when user tap on single character predictive text
I am doing some customizations in this method e.g. mentions support so after doing some work I update attributedText of TextView on mainThread so before setting attributedText to the textView this method get call for space in predictive text selection case and at that time previous text is not setup yet so it will insert just space.
Two things comes in my mind, somehow handle the call of this method that it should not call twice until text is set to textView
Second somehow ignore the space after predictive text.
if text == " " { return false }
We also can not do this because after that user will not be able to insert spaces.
Predictive texts:
I want to disable manual text entering from one-time-code textField while the user only can tap SMS OTP Code from Keyboard Quicktype Bar.
Another question i got from seeing whatsapp is that their input shows the Keyboard Quicktype bar automatically while mine is not unless i call becomeFirstResponder
How can i achieve this?
Thanks.
You can try this, maybe it works for your use case
Remove textField.isEnabled = false if you added it before
Add textField.delegate = self so we can manage what happens when user adds input
Add textField.becomeFirstResponder() to make the keyboard appear
Then implement this UITextFieldDelegate callback
extension YourViewOrViewController: UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
// Only allow multiple characters to be set like the OTP
// Or define your own logic when you want text to be
// accepted into the text field
return string.count != 1
}
}
Check if this gives you the desired result
I'm working on an iOS assignment submission and I'm required to change the value of one text field and this change should reflect on 4 other text fields. On a normal scenario "editingChanged" action would suffice, but I'm using my own custom keyboard (Just buttons on the same view) therefore this action does not get called. What would be the most efficient way to detect a change in text when the change is done by the controller.
You can have a variable where you store the value in the ViewController and then in the didSet of that value do the necessary changes:
var value: String {
didSet {
// do the changes of the others UITextfields here.
}
}
You should set the delegate of the UITextField, and then implement func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool.
Within the body of this function you can change the value of the other text fields
I have created a new project that only has one text field and I set the capitalization to all characters. I tried this from both interface builder and code:
[self.textField setAutocapitalizationType:UITextAutocapitalizationTypeAllCharacters];
No matter what I try, this is the result:
I am aware that the keyboard Auto-Capitalization settings can be changed from Settings - General - Keyboard - Auto-Capitalization, but I assume there would be no purpose in having the AutocapitalizationType property on a text field if it is overwritten by the iOS anyway.
It is also not working for UITextAutocapitalizationTypeWords.
This is happening on iOS 10.0.2 on an iPhone 6S (other answers say that it happens in simulator, but this is not the case).
Any idea what is the issue?
I am aware that the keyboard Auto-Capitalization settings can be
changed from Settings - General - Keyboard - Auto-Capitalization, but
I assume there would be no purpose in having the
AutocapitalizationType property on a text field if it is overwritten
by the iOS anyway.
You're right, the this switch in settings should be on. But it does not override the value of textfields in an app. If this toggle is on, all textfields which want to capitilize user's input will be allowed to do so and textfields with UITextAutocapitalizationTypeNone value won't capitalize anything.
One way I enforced an all-caps presentation in textField regardless of the autocapitalization directive, was to insert this little twist in the UITextFieldDelegate func:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let str = string.capitalized
let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: str) ?? str
textField.text = result
return false
}
This will capitalize inserted characters too, as expected.
I'm developing an app for Ipad. I'm designing a forgot password screen to allow user to enter password to UITextField. By design, the password only allow numeric input. I can set UITextFiled keyboardtype to be phonepad in Iphone but the option seem not working for Ipad (Ipad always show the full keyboard layout). How can we achieve the keyboard for Ipad app that only have number?
Do I have to design my own keyboard layout?
Any help is much appreciate. Thanks!
The keyboard type does not dictate what sort of input the textfield accepts, even if you use a custom keyboard that only displays numbers, the user can always paste something or use an external hardware keyboard.To do that, you need to observe the input, for example, by becoming the UITextFieldDelegate and then:
Example in swift:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
// non decimal digit character set, better save this as a property instead of creating it for each keyboard stroke
let non_digits = NSCharacterSet.decimalDigits.inverted
// Find location for non digits
let range = string.rangeOfCharacter(from: non_digits)
if range == nil { // no non digits found, allow change
return true
}
return false // range was valid, meaning non digits were found
}
This will prevent any non digit character from being added to the textfield.
There is not a built in number-only (phone/pin) keyboard on iPad
You need to implement your own keyboard if you want this on iPad.
There are many examples out there:
https://github.com/azu/NumericKeypad
https://github.com/lnafziger/Numberpad
https://github.com/benzado/HSNumericField
Yes I was facing the same for iPad hence I used this:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// to avoid any other characters except digits
return string.rangeOfCharacter(from: CharacterSet(charactersIn:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!##$%^&*()-=_+`~[{]}|\\: ;\"/?>.<,'")) == nil
}