Created
November 24, 2012 16:15
-
-
Save pyanfield/4140335 to your computer and use it in GitHub Desktop.
Get the current text in the UITextField when UITextField has been changed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string | |
{ | |
NSString *currentStr; | |
if ((range.length == 0 && string.length > 0) || (range.length > 0 && string.length > 0)) { | |
// add the characters to textfield | |
NSString *firstHalfStr = [textField.text substringToIndex:range.location]; | |
NSString *lastHalfStr = [textField.text substringFromIndex:(range.location + range.length)]; | |
currentStr = [NSString stringWithFormat:@"%@%@%@",firstHalfStr,string,lastHalfStr]; | |
}else{ | |
// delete characters from textfield | |
NSString *firstHalfStr = [textField.text substringWithRange:NSMakeRange(0,range.location)]; | |
NSString *lastHalfStr = [textField.text substringFromIndex:(range.location+range.length)]; | |
currentStr = [NSString stringWithFormat:@"%@%@",firstHalfStr,lastHalfStr]; | |
} | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment