Created
November 30, 2015 10:33
-
-
Save YGeorge/d331e9a5711c1ddc92d9 to your computer and use it in GitHub Desktop.
UITextField percent
This file contains 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 { | |
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