Skip to content

Instantly share code, notes, and snippets.

@YGeorge
Created November 30, 2015 10:33
Show Gist options
  • Save YGeorge/d331e9a5711c1ddc92d9 to your computer and use it in GitHub Desktop.
Save YGeorge/d331e9a5711c1ddc92d9 to your computer and use it in GitHub Desktop.
UITextField percent
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.percentTextField) {
NSString *text = textField.text;
if ([string length] == 0 && range.length > 0) { //if delete last symbol
if (text.length > 0) {
text = [text substringToIndex:[text length] - 2];
textField.text = text.length > 0 ? [NSString stringWithFormat:@"%@%%",text] : @"";
}
} else {
text = [text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:@"%" withString:@""];
textField.text = [NSString stringWithFormat:@"%@%%",text];
}
return NO;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment