要运行示例项目,请克隆仓库,并首先从示例目录中运行 pod install
。
您可以设置自定义格式,如 @"(123) 1234:123" 或 @"123-123-12:12"
[[HBPhoneNumberFormatter alloc] initWithFormatting:@"(111) 1111-111"]
如果您需要在 textField 中使用前缀,可以使用此 HBPhoneNumberFormatter 方法。
- (void)setCountryName:(NSString *)name textField:(UITextField *)textField;
在这个类 HBCountryCode
中您可以看到所有的国家名称
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//check textField
if ([textField isEqual:self.phoneField]) {
HBPhoneNumberFormatter *formatter = [[HBPhoneNumberFormatter alloc] initWithFormatting:@"(111) 1111-111"];
return [formatter textField:textField shouldChangeCharactersInRange:range replacementString:string];
}
return YES;
}
isShake, (default == YES)
shakeSize, (default == 5)
shakeDuration, (default == 0.1)
shakeRepeatCount, (default == 2)
示例
HBPhoneNumberFormatter *formatter = [[HBPhoneNumberFormatter alloc] initWithFormatting:@"(111) 1111-111"];
formatter.shakeSize = 10;
formatter.shakeRepeatCount = 4;
PhoneNumberTextField 自动格式化电话号码并提供用户完全编辑功能。
您可以通过以下代码检查 textField 验证 - (BOOL)numberIsValidPhoneText:(NSString *)phoneText;
HBPhoneNumberFormatter 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod 'HBPhoneNumberFormatter', '~> 1.6'
HaykBrsoyan, [email protected]
HBPhoneNumberFormatter 根据 MIT 许可证可用。有关更多信息,请参阅 LICENSE 文件。