Swift: Disable or Clear Undo for a TextField - ios

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
}

Related

In a Swift UITextFieldDelegate how to trigger an action when the text is edited?

I have a ViewController which inherits from UITextFieldDelegate and serves as the delegate for the text field in the view. I would like to trigger an action whenever the text is edited.
I tried implementing the textField method, which is listed in the UITextFieldDelegate documentation:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
The documentation describes this textField method as triggered when the text should be changed, which occurs before the specified text is changed. Here is the line from the documentation:
Asks the delegate if the specified text should be changed.
As this textField method triggers before the text is changed rather than after, the text available at textField.text therefore contains the prior text, not the new text updated after the change. To access the new text, I would need to apply the range and replacementString parameters to textField.text to imitate how the text will be changed.
Is there an alternative to the textField method that is triggered not when the text will be changed, but instead when the text has been changed? I'm thinking that directly using the text after the change would prevent me from having to hack an imitation of the change beforehand.
Add a target with a selector for valueChanged and you'll be able to get the events like you need here.
// adding target
textField.addTarget(self, action: #selector(textChanged), for: .valueChanged)
// selector
#objc func textChanged(_ textField: UITextField) {
print(textField.text)
}
Have you tried textDidChangeNotification? you will have to add observer for keyboarddidshow(notification).
For ur Ref:
https://stackoverflow.com/a/52325593
Just write extension UITextfieldDelegate to viewController, you will have these pre-defined functions:
func textFieldDidBeginEditing(_ textField: UITextField) {
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
}

UITextField no callback when autocorrect changes text

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.

iOS, Swift, UITextField Changing Colour of Text being typed using typingAttributes, first letter's colour is not changed

I'm trying to change the colour of text being typed to white in UITextfield. I've used following code, which works but it doesn't change the colour of first letter to white. See the attached screenshot.
How to make it work so that colour of first letter also changes to white?
I've searched a lot but couldn't find the solution.
Tx in advance!
#IBAction func emailFieldEditingChanged(_ sender: UITextField) {
emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
}
You'll want to set the delegate as Rakesha mentioned, but you have to change it each time the user inputs a character. Use this delegate method.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
return true
}
//Set this in viewDidLoad, and don't forget to include the delegate.
emailTextField.delegate = self

how to monitor keyboard event in iOS

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.

iOS: Most stable way to handle text field/view interactions and keyboard movements?

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.

Resources