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.
Related
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.
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.
When an entry is made in into an UITextField, that has secure text entry enabled.
The last character that is input is given a short preview for a second or so.
I want to disable that as well. Thus, as soon as a character is entered the black dot appears without the preview.
This can be accomplished by configuring the field's UITextFieldDelegate like this:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
return false
}
How do i pass the value of the text from the UITextField into another view controller. Currently, when I override the func prepareForSegue(), it is being called before the user finished editing the textfield, so the value passed is always null.
You have a couple of ways to go about this:
Directly access the text value of your TextField instead of your model, which sounds like it is updated after didFinishEditing
Instead of updating your model only on didFinishEditing you can update it every time the text changes by implementing internal func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool, and getting the new value with let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
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
}