I added a target with editingChanged to the UITextField like this:
self.addTarget(self, action: #selector(self.textChanged), for: .editingChanged)
But if you are typing and iOS suggests the autocorrect and you tap next to where you are typing in the UITextField, the suggestion gets placed into the UITextField, but I did not get notified by the editingChanged.
I had the same problem with the UITextView and managed to "fix" it by setting the delegate and overriding
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
So I tried doing the same with the UITextField and expected to get the same result. So I set the delegate and override
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
However, still no callback when the autocorrect gets applied.
As an example, this is the autocorrect I'm talking about:
If you click on the field next to the word you're typing right now, the autocorrect gets applied. But no callback.
Does anybody have a solution for this?
For anybody looking for a solution here. I don't have a solution. I kept the editingChanged and added editingDidEnd. And made sure I always called self.view.endEditing(true) before I accessed the data. Not the best solution, but for now the only one I got.
Related
When using func textViewDidChange(_ textView: UITextView) it doesn't register text changes triggered by autocorrect when tapping somewhere (to accept the autocorrect suggestion bubble)
When using func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool it takes double space as two spaces and not as period+space like it's configured by default as "." Shortcut in Keyboard Settings.
Using both these methods will lead to duplicate calls.
Is there a method to get all text changes?
Now I have a question is about how to observer keyboard touch in iOS.for example, if I click the 'A','B',or any key in keyboard ,how can I monitor it.I try to add an gestureRecognizer in keyboard. I can monitor it in iOS 7, iOS 9.In iOS 10, it can't work.I don't know why,so I look for another way to implement the function.
You can get entered character from
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
Print(string)
Return true
}
Delegate method. You must need to set delegate of textfield you are entering text to.
I have an app that has a mix of text fields and text views, and people interact with them in various ways. Some switch from entry field to entry field, some type into the field and hit "done," etc.
Has anyone found a way they particularly like to deal with switching between fields/views, while updating internal data models based on the inputted data as it happens? Any favorite tricks?
Relatedly: if somebody switches from one field to another, what is the order of delegate calls (the should/did begin/end editing) for each field, and how does resignFirstResponder() play into it?
Thanks!
You can set unique tags to each UITextFields and UITextViews to differentiate and store data accordingly for each UITextField or UITextView. Whenever someone interacts with an UITextField, the following delegate methods can be used
func textViewDidBeginEditing(textView: UITextView)
func textFieldDidBeginEditing(textView: UITextField)
The above two delegate functions are called whenever we start editing the UITextField or UITextView. These functions can be used to adjust your layout accordingly so that your UITextField or UITextView doesn't go behind the Keyboard. However, in case of multiple UITextFields and UITextViews in the view, it' better to use a third party library to do this task. I used this https://github.com/michaeltyson/TPKeyboardAvoiding in one of my projects
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
Using the above two functions, you can track the changes made to the text inside the UITextField and UITextView
func textViewDidEndEditing(textView: UITextView)
func textFieldDidEndEditing(textField: UITextField)
The above two functions are called whenever editing ends in a particular UITextView or an UITextField. This can be used for storing data from UITextField or UItextView according to tag.
func textFieldShouldReturn(textField: UITextField) -> Bool
This is called when return key is clicked. You can also use this to store the text typed in this textfield in an array according to tag etc.
iOS has a built-in functionality where user can set up text replacements in
Settings > General > Keyboards > Text Replacement
In my Swift iOS app's UITextField, I need to know when user used this text replacement so I can take appropriate action. My instinct was to use
func textField(tf: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
but interestingly, this function is not invoked on this kind of text replacement, are there any other ways to solve this?
I tested the shouldChangeCharactersInRange function in this setting and it does see the text expansion. I tested with:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print(string.characters.count)
return true
}
You can watch the character count jump immediately after the text expansion.
Make sure your textField is hooked up with an IBOutlet and you have set the class as a delegate (textField.delegate = self in viewDidLoad).
Once you see this working you can of course identify text expansion and handle any way you wish.
When selecting NumberPad for a TextField, the iPad displays a full keyboard, so I have created a function to simply remove all characters that are not numeric. However, if a Non-numeric key is pressed and then the user presses Undo on the keyboard, the App Crashes.
How do I disable the Undo function for the TextField or at least clear the Undo Stack?
The answer was simple in the end, I used undoManager removeAllActions, but instead of just needing:
undoManager?.removeAllActions()
as a line, I needed:
myTextField.undoManager?.removeAllActions()
I placed this in the DidChange Action for the TextField.
Add UITextFieldDelegate in your controller.
In your viewDidLoad() assign yourTextField.delegate = self. Than implement this method
public func textFieldShouldClear(textField: UITextField) -> Bool{
return false // This will disable clear button
}
Also for replacing text I would suggest use
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{//Your Logic Here
}