- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString *decimalString = [components componentsJoinedByString:@""];
NSUInteger length = decimalString.length;
BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';
if (length == 0 || (length > 10 && !hasLeadingOne) || (length > 11)) {
textField.text = decimalString;
return NO;
}
NSUInteger index = 0;
NSMutableString *formattedString = [NSMutableString string];
if (hasLeadingOne) {
[formattedString appendString:@"1 "];
index += 1;
}
if (length - index > 3) {
NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
[formattedString appendFormat:@"(%@) ",areaCode];
index += 3;
}
if (length - index > 3) {
NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
[formattedString appendFormat:@"%@-",prefix];
index += 3;
}
NSString *remainder = [decimalString substringFromIndex:index];
[formattedString appendString:remainder];
textField.text = formattedString;
return NO;
}
(OR)
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString *decimalString = [components componentsJoinedByString:@""];
NSUInteger length = decimalString.length;
BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';
if (length == 0 || (length > 10 && !hasLeadingOne) || (length > 11)) {
textField.text = decimalString;
return NO;
}
NSUInteger index = 0;
NSMutableString *formattedString = [NSMutableString string];
if (hasLeadingOne) {
[formattedString appendString:@"1 "];
index += 1;
}
if (length - index > 3) {
NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
[formattedString appendFormat:@"(%@) ",areaCode];
index += 3;
}
if (length - index > 3) {
NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
[formattedString appendFormat:@"%@-",prefix];
index += 3;
}
NSString *remainder = [decimalString substringFromIndex:index];
[formattedString appendString:remainder];
textField.text = formattedString;
return NO;
}
(OR)
[self.textField addTarget:self action:@selector(formatPhoneNumber) forControlEvents:UIControlEventEditingChanged];
#pragma mark - phone number formatting
- (void)formatPhoneNumber {
NSString *currentString = self.textField.text;
NSString *strippedValue = [currentString stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, currentString.length)];
NSString *formattedString;
if (strippedValue.length == 0) {
formattedString = @"";
}
else if (strippedValue.length < 3) {
formattedString = [NSString stringWithFormat:@"(%@", strippedValue];
}
else if (strippedValue.length == 3) {
formattedString = [NSString stringWithFormat:@"(%@) ", strippedValue];
}
else if (strippedValue.length < 6) {
formattedString = [NSString stringWithFormat:@"(%@) %@", [strippedValue substringToIndex:3], [strippedValue substringFromIndex:3]];
}
else if (strippedValue.length == 6) {
formattedString = [NSString stringWithFormat:@"(%@) %@-", [strippedValue substringToIndex:3], [strippedValue substringFromIndex:3]];
}
else if (strippedValue.length <= 10) {
formattedString = [NSString stringWithFormat:@"(%@) %@-%@", [strippedValue substringToIndex:3], [strippedValue substringWithRange:NSMakeRange(3, 3)], [strippedValue substringFromIndex:6]];
}
else if (strippedValue.length >= 11) {
formattedString = [NSString stringWithFormat:@"(%@) %@-%@ x%@", [strippedValue substringToIndex:3], [strippedValue substringWithRange:NSMakeRange(3, 3)], [strippedValue substringWithRange:NSMakeRange(6, 4)], [strippedValue substringFromIndex:10]];
}
self.textField.text = formattedString;
}
No comments:
Post a Comment