Giter Site home page Giter Site logo

wecognize / flagphonenumber Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chronotruck/flagphonenumber

0.0 2.0 0.0 14.93 MB

A formatted phone number UITextField with country flag picker.

License: Apache License 2.0

Swift 98.16% Ruby 1.79% Shell 0.05%

flagphonenumber's Introduction

FlagPhoneNumber

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

CI Status Version License Platform Language

๐ŸŒ… Screenshot

๐ŸŒ 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 and Carthage.

Carthage

Simply add in your Cartfile the following line:

github "chronotruck/FlagPhoneNumber"

Cocoapods

Simply add in your Podfile the following line:

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")

๐Ÿšจ 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])

Or exclude countries from the list:

phoneNumberTextField.setCountries(excluding: [.AM, .BW, .BA])

โœจ Next Improvments

  • Localization
  • Country search
  • Placeholder
  • Exclude/Include 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

Open source time proudly sponsored by Chronotruck.

๐Ÿ’๐Ÿปโ€โ™‚๏ธ 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.

flagphonenumber's People

Contributors

grifas avatar fvolchyok avatar qaavi avatar nickenchev avatar akilanarasu1994 avatar jmartinesp avatar gadaxara avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.