Skip to content

A formatted phone number Picker and country picker with flags

License

Notifications You must be signed in to change notification settings

moKelani/FlagPhoneNumber

 
 

Repository files navigation

FlagPhoneNumber

FlagPhoneNumber is a phone number textfield with a fancy country code picker.

CI Status Version License Platform Language

🌅 Screenshot

Watch the video

🌍 Localization

Country names are displayed according to the phone language

📲 Example

you can see full example with cocoapods to see how it work.

⬇️ Installation

Cocoapods

Simply add in your Podfile the following line:

pod 'FlagPhoneNumber', :git => 'https://github.com/moKelani/FlagPhoneNumber'

Manually

Simply import the Sources repository in your project.

👴🏻 Objective-C is supported

🗺 Usage

You can instantiate it in storyboards/xibs or programmatically:

let phoneNumberTextField = FPNTextField(frame: CGRect(x: 0, y: 0, width: view.bounds.width - 16, height: 50))

// You can change the chosen flag then set the phone number
phoneNumberTextField.setFlag(for: .FR)
phoneNumberTextField.set(phoneNumber: "0600000001")

// Or directly set the phone number with country code, which will update automatically the flag image
phoneNumberTextField.set(phoneNumber: "+33600000001")

🚨 FPNTextFieldDelegate

FPNTextFieldDelegate inherites from UITextFieldDelegate so nothing change:

phoneNumberTextField.delegate = self

It provides two methods that lets you know when a country is selected and when the phone number is valid or not. Once a phone number is valid, you can get it in severals formats (E164, International, National, RFC3966):

extension YourViewController: FPNTextFieldDelegate {

   func fpnDidSelectCountry(name: String, dialCode: String, code: String) {
      print(name, dialCode, code) // Output "France", "+33", "FR"
   }

   func fpnDidValidatePhoneNumber(textField: FPNTextField, isValid: Bool) {
      if isValid {
         // Do something...         
         textField.getFormattedPhoneNumber(format: .E164),           // Output "+33600000001"
         textField.getFormattedPhoneNumber(format: .International),  // Output "+33 6 00 00 00 01"
         textField.getFormattedPhoneNumber(format: .National),       // Output "06 00 00 00 01"
         textField.getFormattedPhoneNumber(format: .RFC3966),        // Output "tel:+33-6-00-00-00-01"
         textField.getRawPhoneNumber()                               // Output "600000001"
      } else {
         // Do something...
      }
   }
}

🎨 Customization

FlagKit is used by default but you can customize the list with your own flag icons assets:

// Be sure to set it before initializing a FlagPhoneNumber instance.
Bundle.FlagIcons = YOUR_FLAG_ICONS_BUNDLE

You can change the size of the flag:

phoneNumberTextField.flagSize = CGSize(width: 44, height: 44)

You can change the edge insets of the flag:

phoneNumberTextField.flagButtonEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10)

If you set the parentViewController programmatically or from @IBOutlet, a search button appears in the picker inputAccessoryView to present a country search view controller:

phoneNumberTextField.parentViewController = self // or from @IBOutlet

You can customize the inputAccessoryView of the textfield:

phoneNumberTextField.textFieldInputAccessoryView = getCustomTextFieldInputAccessoryView(with: items)

You can also customize the flag button's properties:

// This will freeze the flag.
// Only one particular country's phone numbers will be formatted and validated.
// You can set the country by setting the flag as shown earlier.
phoneNumberTextField.flagButton.isUserInteractionEnabled = false

You can have in the placeholder an example of a phone number according to the selected country or have your own placeholder:

phoneNumberTextField.hasPhoneNumberExample = false // true by default
phoneNumberTextField.placeholder = "Phone Number"

You can choose which country can appears in the list:

phoneNumberTextField.setCountries(including: [.FR, .ES, .IT, .BE, .LU, .DE])

About

A formatted phone number Picker and country picker with flags

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 46.4%
  • Objective-C 34.8%
  • Swift 18.6%
  • Ruby 0.2%