Latest 0.6.1
Homepage https://github.com/chronotruck/FlagPhoneNumber
License Apache License 2.0
Platforms ios 8.0
Dependencies libPhoneNumber-iOS
Authors

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

CI Status
Version
License
Platform
Language

🌅 Screenshot

FlagPhoneNumber FlagPhoneNumber FlagPhoneNumber

🌍 Localization

Country names are displayed according to the phone language

📲 Example

To run the example project, clone the repo, and run pod install from the Example directory first.

⬇️ Installation

FlagPhoneNumber is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "FlagPhoneNumber"

🗺 Usage

You can instantiate it in storyboards/xibs or programmatically:

let phoneNumberTextField = FlagPhoneNumberTextField(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")

// By default, an example of a phone number according to the selected country is displayed in the placeholder. You can use your own placeholder:
phoneNumberTextField.hasPhoneNumberExample = false // Default true
phoneNumberTextField.placeholder = "Phone Number"

🚨 Delegate

FlagPhoneNumberTextField provides you a FPNTextFieldDelegate.

It lets you know you when a country is selected

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

or when the phone number is valid or not:

func didValidatePhoneNumber(textField: FPNTextField, isValid: Bool) {
  if isValid {
    // Do something...
    } else {
      // Do something...
    }

Once a phone number is valid, you can get it in severals formats (E164, International, National):

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"

🎨 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, a search button appears in the picker inputAccessoryView to present a country search view controller:

phoneNumberTextField.parentViewController = self

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

✨ Next Improvments

  • [x] Localization
  • [x] Country search
  • [x] Placeholder
  • [ ] Exclude Countries
  • [ ] Any idea ?

☕️ Conception

This library is high inspired of MRCountryPicker library and use libPhoneNumber-iOS library.
https://github.com/xtrinch/MRCountryPicker / https://github.com/iziz/libPhoneNumber-iOS

💁🏻‍♂️ Author

grifas, [email protected]

Don’t hesitate to contact me or make a pull request to upgrade this library.

📝 License

FlagPhoneNumber is available under the Apache license. See the LICENSE file for more info.

Latest podspec

{
    "name": "FlagPhoneNumber",
    "version": "0.6.1",
    "summary": "A formatted phone number UITextField with country flag picker.",
    "description": "FlagPhoneNumber is a phone number textfield that allows you to choose the country code thanks to a picker. It uses libPhoneNumber to format the number in the textfield according to country code.",
    "homepage": "https://github.com/chronotruck/FlagPhoneNumber",
    "license": {
        "type": "Apache License 2.0",
        "file": "LICENSE"
    },
    "authors": {
        "Auru00e9lien Grifasi": "[email protected]"
    },
    "source": {
        "git": "https://github.com/chronotruck/FlagPhoneNumber.git",
        "tag": "0.6.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "FlagPhoneNumber/**/*.swift",
    "resource_bundles": {
        "FlagPhoneNumber": [
            "FlagPhoneNumber/Resources/**/*"
        ]
    },
    "dependencies": {
        "libPhoneNumber-iOS": []
    }
}

Pin It on Pinterest

Share This