SHSPhoneComponent 2.32

SHSPhoneComponent 2.32

测试已测试
语言语言 Obj-Cobjective-C
许可 MIT
发布最后发布2018年12月

Serheo ShatunovSergei Shatunov 维护。



  • Serheo Shatunov

SHSPhoneComponent

UITextField 和 NSFormatter 的子类用于格式化电话号码。允许不同国家的不同格式(模式)。光标定位优秀。

Swift 版本在这里 - https://github.com/Serheo/PhoneNumberFormatter

如何安装

使用以下任何一种方法

  • 使用内嵌框架 /SHSPhoneComponents/SHSPhoneComponent.xcodeproj (iOS 8+)
  • pod 'SHSPhoneComponent'
  • 将 /SHSPhoneComponents/Library 文件夹复制到您的项目中。

并在您的控制器中导入 "SHSPhoneLibrary.h"。

示例用法

如果您需要完整的示例,请参阅 'Example_iOS7+' 或 'Example_iOS8+embedded' 文件夹。

默认格式

[self.phoneField.formatter setDefaultOutputPattern:@"+# (###) ###-##-##"];

shspc example 1

所有输入字符串都将以此方式解析。例如:+7 (920) 123-45-67

前缀格式

您可以为所有输入设置前缀

[self.phoneField.formatter setDefaultOutputPattern:@"(###) ###-##-##"];
self.phoneField.formatter.prefix = @"+7 ";

shspc example 1

多种格式

[self.phoneField.formatter setDefaultOutputPattern:@"##########" imagePath:nil];
[self.phoneField.formatter addOutputPattern:@"+# (###) ###-##-##" forRegExp:@"^7[0-689]\\d*$" imagePath:@"flagRU"];
[self.phoneField.formatter addOutputPattern:@"+### (##) ###-###" forRegExp:@"^374\\d*$" imagePath:@"flagAM"];

shspc example 2

带前缀的多种格式

[self.phoneField.formatter setDefaultOutputPattern:@"### ### ###"];
self.phoneField.formatter.prefix = @"+7 ";
[self.phoneField.formatter addOutputPattern:@"(###) ###-##-##" forRegExp:@"^1\\d*$" imagePath:@"SHSPhoneImage.bundle/flag_ru"];
[self.phoneField.formatter addOutputPattern:@"(###) ###-###" forRegExp:@"^2\\d*$" imagePath:@"SHSPhoneImage.bundle/flag_ua"];

特定格式

如果您想以特定方式格式化一些数字,请这样做

[self.phoneField.formatter addOutputPattern:@"+# (###) ###-##-##" forRegExp:@"^7[0-689]\\d*$" imagePath:@"flagRU"];
[self.phoneField.formatter addOutputPattern:@"+### (##) ###-###" forRegExp:@"^374\\d*$" imagePath:@"flagAM"];

格式化

如果您只需格式化功能,则可以使用SHSPhoneNumberFormatter类。有关类信息的附加信息,请参阅 http://serheo.github.io/SHSPhoneComponent/

问题和解决方案

如果您在任何文本字段中使用任何预测/建议,请将hasPredictiveInput标志设置为YES。

要求

启用ARC。iOS 7+

许可协议

SHSPhoneComponent 适用于 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。