如何在 iOS 中针对不同的区域代码实时格式化 phone 数字(使用 UITextfield 时)?
How to format phone number realtime (when using UITextfield) in iOS for different region codes?
我找到了“NBAsYouTypeFormatter”并参考了提供的演示。但是,当用户在 UITextField 中输入 phone 数字时,我希望动态格式化 phone 数字。
请记住,在我的案例中,区域代码并不固定为一个或两个国家,例如仅@"US" 或仅其他。区域代码将由用户从下拉列表中选择,所选代码将由我通过以下行传递:
NBAsYouTypeFormatter *asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"]; // may be any other than 'US'
NSString *output = [asYouTypeFormatter inputString:@"9999988888"];
现在,当用户在数字文本字段中键入时,我如何能够动态地格式化数字文本字段?
在 UITextField 委托的方法中
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *totalString = [NSString stringWithFormat:@"%@%@",textField.text,string];
if (range.length == 1)
{
// Delete button was hit.. so tell the method to delete the last char.
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\s-\(\)]" options:NSRegularExpressionCaseInsensitive error:&error];
totalString = [regex stringByReplacingMatchesInString:totalString options:0 range:NSMakeRange(0, [totalString length]) withTemplate:@""];
totalString = [totalString substringToIndex:[totalString length] - 1];
}
textField.text = [Utility formatPhoneNumber:totalString countryCode:@"theCountryCode"];
return NO;
}
此函数将根据提供的国家/地区代码将您的字符串格式化为 phone 格式数字。
+ (NSString *)formatPhoneNumber:(NSString *)simpleNumber countryCode:(NSString *)countryCode
{
if (simpleNumber.length == 0)
{
return @"";
}
NSInteger maxLen = 15;
// use regex to remove non-digits(including spaces) so we are left with just the numbers
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^[0-9+]]" options:NSRegularExpressionCaseInsensitive error:&error];
simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""];
// check if the number is to long
if (simpleNumber.length > maxLen)
{
// remove last extra chars.
simpleNumber = [simpleNumber substringToIndex:maxLen];
}
NSString *firstChar = @"";
BOOL countryCodeLen = countryCode.length;
if (simpleNumber.length > 0)
{
firstChar = [simpleNumber substringToIndex:1];
}
if ([firstChar isEqualToString:@"+"])
{
//+1 (234)
if (simpleNumber.length < 5 + countryCodeLen)
{
NSString *string = [NSString stringWithFormat:@"(\d{%d})(\d+)",countryCodeLen];
// simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{1})(\d+)"
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:string
withString:@" ()"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
//+1 (234) 567
else if(simpleNumber.length < 8 + countryCodeLen)
{
NSString *string = [NSString stringWithFormat:@"(\d{%d})(\d{3})(\d+)",countryCodeLen];
// simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{1})(\d{3})(\d+)"
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:string
withString:@" () "
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
//+1 (234) 567-
else // else do this one..
{
NSString *string = [NSString stringWithFormat:@"(\d{%d})(\d{3})(\d{3})(\d+)",countryCodeLen];
// simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{1})(\d{3})(\d{3})(\d+)"
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:string
withString:@" () -"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
}
else
{
// 123 456 7890
// format the number.. if it's less then 7 digits.. then use this regex.
if (simpleNumber.length < 7)
{
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{3})(\d+)"
withString:@"() "
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
else // else do this one..
{
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{3})(\d{3})(\d+)"
withString:@"() -"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
}
return simpleNumber;
}
希望对您有所帮助
我是这样实现的。我将从头开始解释。所以,新用户可以从头开始。
从 here 下载 libPhoneNumber-iOS 库。在该 link 页面的底部,您会找到需要添加到项目中的文件。
您的捆绑包应如下所示:
现在,按照以下步骤实施。
(1) 在需要格式化文本字段的视图控制器中导入文件。
#import "NBPhoneMetaDataGenerator.h"
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"
并在头文件中创建 NBAsYouTypeFormatter 类型的实例:
NBAsYouTypeFormatter *asYouTypeFormatter;
(2) 在该视图控制器的 viewDidLoad 方法中,初始化之前获取的对象:
asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"IN"];
注意:@"IN" 适用于印度。您可以将其设置为任何您想要的。请参阅将包含在 libPhoneNumber-iOS 库中的 plist 文件以查看区域代码的完整列表。
(3)在UITextField的delegate方法中,动态管理你的textfield的文本。
#pragma mark
#pragma mark - Phone Number textfield formatting
# define LIMIT 18 // Or whatever you want
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Just allow 18 digits
if(!(([string length] + range.location) > LIMIT))
{
// Something entered by user
if(range.length == 0)
{
[textNumber setText:[self.asYouTypeFormatter inputDigit:string]];
}
// Backspace
else if(range.length == 1)
{
[textNumber setText:[self.asYouTypeFormatter removeLastDigit]];
}
}
return NO;
}
希望对大家有帮助!!!
我找到了“NBAsYouTypeFormatter”并参考了提供的演示。但是,当用户在 UITextField 中输入 phone 数字时,我希望动态格式化 phone 数字。
请记住,在我的案例中,区域代码并不固定为一个或两个国家,例如仅@"US" 或仅其他。区域代码将由用户从下拉列表中选择,所选代码将由我通过以下行传递:
NBAsYouTypeFormatter *asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"]; // may be any other than 'US'
NSString *output = [asYouTypeFormatter inputString:@"9999988888"];
现在,当用户在数字文本字段中键入时,我如何能够动态地格式化数字文本字段?
在 UITextField 委托的方法中
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *totalString = [NSString stringWithFormat:@"%@%@",textField.text,string];
if (range.length == 1)
{
// Delete button was hit.. so tell the method to delete the last char.
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\s-\(\)]" options:NSRegularExpressionCaseInsensitive error:&error];
totalString = [regex stringByReplacingMatchesInString:totalString options:0 range:NSMakeRange(0, [totalString length]) withTemplate:@""];
totalString = [totalString substringToIndex:[totalString length] - 1];
}
textField.text = [Utility formatPhoneNumber:totalString countryCode:@"theCountryCode"];
return NO;
}
此函数将根据提供的国家/地区代码将您的字符串格式化为 phone 格式数字。
+ (NSString *)formatPhoneNumber:(NSString *)simpleNumber countryCode:(NSString *)countryCode
{
if (simpleNumber.length == 0)
{
return @"";
}
NSInteger maxLen = 15;
// use regex to remove non-digits(including spaces) so we are left with just the numbers
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^[0-9+]]" options:NSRegularExpressionCaseInsensitive error:&error];
simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""];
// check if the number is to long
if (simpleNumber.length > maxLen)
{
// remove last extra chars.
simpleNumber = [simpleNumber substringToIndex:maxLen];
}
NSString *firstChar = @"";
BOOL countryCodeLen = countryCode.length;
if (simpleNumber.length > 0)
{
firstChar = [simpleNumber substringToIndex:1];
}
if ([firstChar isEqualToString:@"+"])
{
//+1 (234)
if (simpleNumber.length < 5 + countryCodeLen)
{
NSString *string = [NSString stringWithFormat:@"(\d{%d})(\d+)",countryCodeLen];
// simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{1})(\d+)"
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:string
withString:@" ()"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
//+1 (234) 567
else if(simpleNumber.length < 8 + countryCodeLen)
{
NSString *string = [NSString stringWithFormat:@"(\d{%d})(\d{3})(\d+)",countryCodeLen];
// simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{1})(\d{3})(\d+)"
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:string
withString:@" () "
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
//+1 (234) 567-
else // else do this one..
{
NSString *string = [NSString stringWithFormat:@"(\d{%d})(\d{3})(\d{3})(\d+)",countryCodeLen];
// simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{1})(\d{3})(\d{3})(\d+)"
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:string
withString:@" () -"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
}
else
{
// 123 456 7890
// format the number.. if it's less then 7 digits.. then use this regex.
if (simpleNumber.length < 7)
{
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{3})(\d+)"
withString:@"() "
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
else // else do this one..
{
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{3})(\d{3})(\d+)"
withString:@"() -"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
}
}
return simpleNumber;
}
希望对您有所帮助
我是这样实现的。我将从头开始解释。所以,新用户可以从头开始。
从 here 下载 libPhoneNumber-iOS 库。在该 link 页面的底部,您会找到需要添加到项目中的文件。
您的捆绑包应如下所示:
现在,按照以下步骤实施。
(1) 在需要格式化文本字段的视图控制器中导入文件。
#import "NBPhoneMetaDataGenerator.h"
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"
并在头文件中创建 NBAsYouTypeFormatter 类型的实例:
NBAsYouTypeFormatter *asYouTypeFormatter;
(2) 在该视图控制器的 viewDidLoad 方法中,初始化之前获取的对象:
asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"IN"];
注意:@"IN" 适用于印度。您可以将其设置为任何您想要的。请参阅将包含在 libPhoneNumber-iOS 库中的 plist 文件以查看区域代码的完整列表。
(3)在UITextField的delegate方法中,动态管理你的textfield的文本。
#pragma mark
#pragma mark - Phone Number textfield formatting
# define LIMIT 18 // Or whatever you want
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Just allow 18 digits
if(!(([string length] + range.location) > LIMIT))
{
// Something entered by user
if(range.length == 0)
{
[textNumber setText:[self.asYouTypeFormatter inputDigit:string]];
}
// Backspace
else if(range.length == 1)
{
[textNumber setText:[self.asYouTypeFormatter removeLastDigit]];
}
}
return NO;
}
希望对大家有帮助!!!