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]];
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
}
}
Limiting characters and character length in textField(_:shouldChangeCharactersInRange:replacementString:)
Try this:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == usernameField, let text = textField.text {
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 48 && !string.containsString("|")
}
return true
}
UITextField - Add a character/s after 1st character is entered while typing
-shouldChangeCharactersInRange gets called before the change to the text field takes place, so the length is still 0 (see Using `textField:shouldChangeCharactersInRange:`, how do I get the text including the current typed character?). Try this instead:
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range
replacementString: (NSString*) string {
if (textField == currHeight) {
NSString *text = [textField.text stringByReplacingCharactersInRange:range
withString: string];
if (text.length == 1) { //or probably better, check if int
textField.text = [NSString stringWithFormat: @"%@ ft ", text];
return NO;
}
}
return YES;
}
Make portion of UITextView undeletable
sch's last edit makes a decent answer, but I want to offer a slightly more flexible approach.
You have to keep in mind the copy/paste system. User might select all the text in text field and try to paste in the entire value which might be perfectly acceptable, but if (range.location <= 9) { return NO; }
will reject it. The way I'd do it is put together a string that would be a result of successful edit and then check if that string would start with your desired prefix.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(@"resulting string would be: %@", resultString);
NSString *prefixString = @"blabla";
NSRange prefixStringRange = [resultString rangeOfString:prefixString];
if (prefixStringRange.location == 0) {
// prefix found at the beginning of result string
return YES;
}
return NO;
}
Edit: if you want to check if the current string in text field starts with the prefix, you can use rangeOfString:
the same way:
NSRange prefixRange = [textField.text rangeOfString:prefixString];
if (prefixRange.location == 0) {
// prefix found at the beginning of text field
}
is it possible to know if user is typing or deleting characters in a textfield?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.length > 0)
{
// We're deleting
}
else
{
// We're adding
}
}
How do I get the value of a UITextfield as characters are being typed in real time?
Try this:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let invalidCharacters = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_").invertedSet
if let range = string.rangeOfCharacterFromSet(invalidCharacters, options: nil, range:Range<String.Index>(start: string.startIndex, end: string.endIndex)) {
return false
}
return true
}
Related Topics
How to Use Git Properly with Xcode
Scrollview and Keyboard in Swift
iOS 8 - Screen Blank After Dismissing View Controller with Custom Presentation
Color in Storyboard Not Matching Uicolor
Uiscrollview's Origin Changes After Popping Back to the Uiviewcontroller
How to Turn Flashlight on and Off in Swift
Custom Pin Image in Annotationview in iOS
Custom Font in iOS Not Working
Reload Tableview Section Without Scroll or Animation
Swift Access Control with Target Selectors
How to Control Shadow Spread and Blur
How to Manage Sessions with Afnetworking
How to Ask User for Camera Access After They Have Already Denied It on iOS
Using Convertpoint to Get the Relative Position Inside a Parent Uiview
Improper Advertising Identifier [Idfa] Usage