SHSPhoneComponent
UITextField and NSFormatter subclasses for formatting phone numbers. Allow different formats for different countries(patterns).
Install / Use
/learn @Serheo/SHSPhoneComponentREADME
SHSPhoneComponent <a href="https://travis-ci.org/Serheo/SHSPhoneComponent"><img src="https://travis-ci.org/Serheo/SHSPhoneComponent.png"/></a>
UITextField and NSFormatter subclasses for formatting phone numbers. Allow different formats for different countries(patterns). Caret positioning works excellent.
Swift version is here - https://github.com/Serheo/PhoneNumberFormatter
How To Install
Use any of next methods:
- use embedded framework /SHSPhoneComponents/SHSPhoneComponent.xcodeproj (iOS 8+)
- pod 'SHSPhoneComponent'
- copy /SHSPhoneComponents/Library folder to your project.
And import "SHSPhoneLibrary.h" on your Controller.
Example Usage
If you need complete example please see 'Example_iOS7+' or 'Example_iOS8+embedded' folders.
Default Format
[self.phoneField.formatter setDefaultOutputPattern:@"+# (###) ###-##-##"];
<p align="center">
<img src="http://serheo.github.io/SHSPhoneComponent/readme/r1.jpg" alt="shspc example 1"/>
</p>
All input strings will be parsed in that way.
Example: +7 (920) 123-45-67
Prefix Format
You can set prefix on all inputs:
[self.phoneField.formatter setDefaultOutputPattern:@"(###) ###-##-##"];
self.phoneField.formatter.prefix = @"+7 ";
<p align="center">
<img src="http://serheo.github.io/SHSPhoneComponent/readme/r7.jpg" alt="shspc example 1"/>
</p>
Multiple Formats
[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"];
<p align="center">
<img src="http://serheo.github.io/SHSPhoneComponent/readme/r2.jpg" alt="shspc example 2"/>
</p>
Multiple Formats with prefix
[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"];
Specific Formats
If you want to format some numbers in specific way just do
[self.phoneField.formatter addOutputPattern:@"+# (###) ###-##-##" forRegExp:@"^7[0-689]\\d*$" imagePath:@"flagRU"];
[self.phoneField.formatter addOutputPattern:@"+### (##) ###-###" forRegExp:@"^374\\d*$" imagePath:@"flagAM"];
Formatting
If you need only formatting function you can use SHSPhoneNumberFormatter class. For additional class info see http://serheo.github.io/SHSPhoneComponent/
Issues and Solutions
if you are using any predictions/suggestion in the textfield, set hasPredictiveInput flag to YES.
Requirements
ARC Enabled. iOS 7+
License
SHSPhoneComponent is available under the MIT license. See the LICENSE file for more info.
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
