How shouldChangeCharactersInRange works in Swift?
Swift 4, Swift 5
This method doesn't use NSString
// MARK: - UITextFieldDelegate
extension MyViewController: UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange,
with: string)
myvalidator(text: updatedText)
}
return true
}
}
Note. Be careful when you use a secured text field.
Textfield shouldchangecharactersinrange swift
You could use a target on your textField
with the control event EditingChanged
instead of the delegate method.
Swift >= 1.0
myTextField.addTarget(self, action: "didChangeText:", forControlEvents: .EditingChanged)
Swift 3.0 (String literal selectors are deprecated, use #selector)
myTextField.addTarget(self, action: #selector(didChangeText(_:)), for: .editingChanged)
Then use the targeted method to run your checks.
func didChangeText(textField:UITextField) {
if textField.text == "apple" {
checkImageView1.hidden = false
} else {
checkImageView1.hidden = true
}
}
UITextView shouldChangeCharactersInRange in Swift
Requires UITextViewDelegate reference in class line:
class MyViewController: UIViewController, UITextViewDelegate {
}
AND it also requires a referencing outlet from the view controller to the textView delegate. Select the view controller, click on outlets, drag from referencing outlet to the textView, click delegate.
Using `textField:shouldChangeCharactersInRange:`, how do I get the text including the current typed character?
-shouldChangeCharactersInRange
gets called before text field actually changes its text, that's why you're getting old text value. To get the text after update use:
[textField2 setText:[textField1.text stringByReplacingCharactersInRange:range withString:string]];
UITextField shouldChangeCharactersInRange doesn't detect first character
The shouldChangeCharactersInRange() function is asking whether the change should be applied but it has not yet allowed your textfield to be modified.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if textField == myTextField {
let textString : NSString = textField.text!
let candidateString : NSString = textString.stringByReplacingCharactersInRange(range, withString: string)
let updatedTextString : NSString = PartialFormatter().formatPartial(candidateString as String)
print(updatedTextString)
textField.text = updatedTextString
}
return true
}
textField shouldChangeCharactersInRange is executing one step behind user action
The text hasn't changed yet when this method is called. You have to userange
and replacementString
to construct the email address, and then verify that.
Related Topics
How to Convert an Uiimage to Grayscale in Swift Using Cifilter
Conditionally Import a Framework (Such as Speech) Based on iOS Version in Swift
How to Edit Uialertaction Text Font Size and Color
How to Set Mime Type of Application/Vnd.Apple.Pkpass in Order to Share Pass by Link or Email
Parse Multipart Response for Image Download in iOS
How to Save List of Object in User Default
How to Force an App to Change Language in iOS/Objective-C
How to Get the Incoming Call Number by Using Callkit
How to Implement Login/Logout Navigation Using Userdefaults in Swift
Uitableviewcell Imageview Changing on Select
Swift/How to Use Dispatch_Group with Multiple Called Web Service
Have a Variable with Multiple Types in Swift
How to Convert Bytes to a Float Value in Swift
My Swift 4 Uiscrollview with Autolayout Constraints Is Not Scrolling
How to Get Directions in Mkmapview Using a Built in Apple API
How to Get Animated Polyline Route in Gmsmapview, So That It Move Along with Map When Map Is Moved