An UITextField subclass to simplify country code's picking. Swift 5.0

Overview


Platform Swift version License
CocoaPods CocoaPods

Preview


Installation

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

pod 'NKVPhonePicker'

Also you can try an example project with

pod try NKVPhonePicker

(don't forget to update your cocoapods master repo)

For swift 4.2 version use 011f7090e69a8446aadd21531e4b83da745cd2d4 in master branch.

If you're still using Swift 2.x - you can set (The development for swift_2.x is deprecated. Stale version still exists.)

pod 'NKVPhonePicker', :git => "https://github.com/NikKovIos/NKVPhonePicker.git", :branch => "Swift_2.x"

Usage

  1. Make your UITextField a class of NKVPhonePickerTextField
  2. Set its phonePickerDelegate to UIViewController in order to be able of presenting the CountriesViewController
  3. If any troubles - watch an example project.

Please, make an issue, if you need any features, or have bugs.

Example

topTextField.phonePickerDelegate = self
topTextField.favoriteCountriesLocaleIdentifiers = ["RU", "ER", "JM"]
topTextField.rightToLeftOrientation = true
topTextField.shouldScrollToSelectedCountry = false
topTextField.flagSize = CGSize(width: 30, height: 50)
topTextField.enablePlusPrefix = false

// Setting initial custom country
let country = Country.countryBy(countryCode: "EG")
topTextField.currentSelectedCountry = country

// Setting custom format pattern for some countries
topTextField.customPhoneFormats = ["RU" : "# ### ### ## ##",
                                   "GB": "## #### #########"]

// Adding programmatically
bottomTextField = NKVPhonePickerTextField(frame: CGRect(x: 0, y: 0, width: 0, height: 30))
bottomTextField.placeholder = "ex: 03123456"
bottomTextField.autocorrectionType = .no
bottomTextField.phonePickerDelegate = self
bottomTextField.keyboardType = .numberPad
bottomTextField.favoriteCountriesLocaleIdentifiers = ["LB"]
bottomTextField.layer.borderWidth = 1
bottomTextField.layer.borderColor = UIColor.white.cgColor
bottomTextField.layer.cornerRadius = 5
bottomTextField.font = UIFont.boldSystemFont(ofSize: 25)
bottomTextField.textColor = UIColor.white
bottomTextField.textFieldTextInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
bottomTextField.translatesAutoresizingMaskIntoConstraints = false

// prefilling
let source = NKVSource(countryCode: "ru")
bottomTextField.setCurrentCountryInitially = false
bottomTextField.preFillText(source: source, number: 7999432423)

Note: 'In this library used the TextFieldPatternFormat lib by Vojta Stavik'

TODO:

  • Add example
  • Max numbers count var (can do with custom pattern)
  • Patterns for each country

Checkout my other works

Comments
  • PlaceHolder not shown

    PlaceHolder not shown

    General Information

    What happened?

    Placeholder is not shown even if set it programatically or from storyboard

    What did you expect to happen?

    Placeholder should shown in text feild

    question 
    opened by tjsingh04 13
  • Need a way to distinguish between countries with the same code.

    Need a way to distinguish between countries with the same code.

    Some countries such as U.S. and Canada both start with code +1 and it seems like for this particular example, library defaults to Canada and shows the Canadian flag. Is there a way to modify this behaviour?

    enhancement 
    opened by Saikedo 13
  • Can't set phonePickerDelegate Programatically

    Can't set phonePickerDelegate Programatically

    I believe there is no way to set the phonePickerDelegate as it's not an option while using the picker programmatically. Or I haven't figured out the way.

    bug 
    opened by ibrahimchiha 11
  • Brazilian format is wrong

    Brazilian format is wrong

    We have 2 patterns:

    +55 (XX) XXXXX-XXXX and +55 (XX) XXXX-XXXX

    country code state code (2 digits)
    4 or 5 digits, then 4 digits

    It depends on which state the number is

    enhancement 
    opened by CavalcanteLeo 8
  • Can you help me ?

    Can you help me ?

    @IBOutlet public weak var phonePickerDelegate: UIViewController?
    

    你好 请问这个phonePickerDelegate如何创建?

    how are you How to create this phonePickerDelegate ?

    opened by hong0595 7
  • Wrong Flags

    Wrong Flags

    Hello, there's a problem with the country selection. If i select United States, it shows me the Canada Flag. The selection for UK also shows wrong flag.

    bug 
    opened by alexionut 5
  • Could not build Objective-C module 'NKVPhonePicker'

    Could not build Objective-C module 'NKVPhonePicker'

    General Information

    • NKVPhonePicker Version: 2.0.1
    • iOS Version(s): 10.13.6
    • Swift Version: 4.0
    • Device or Simulator: Phone XR
    • Did you check an example: Yes

    What happened?

    Could not build Objective-C module 'NKVPhonePicker'

    What did you expect to happen?

    opened by okdolly-001 4
  • Is it possible to pre fill the textfield?

    Is it possible to pre fill the textfield?

    I'm trying to pre fill the text field with the current phone number of the user, but the flag is not getting updated and also it's taking the code of US "+1" when the number is "569812312322" that is a Chilean number.

    I'm using textfield.text = "569812312322" to pre fill the text field.

    What happened?

    Flag not getting updated and also wrong country code.

    What did you expect to happen?

    Set the flag to Chile and keep the correct code "56".

    bug enhancement FAQ Done 
    opened by gabrielcandiag 4
  • Selected search Country did not reflect. Search Dismiss crashed

    Selected search Country did not reflect. Search Dismiss crashed

    General Information

    • NKVPhonePicker Version: 2.0.1

    • iOS Version(s): 11

    • Swift Version: 4.1

    • Device or Simulator: Simulator

    • Did you check an example:

    What happened?

    Try to search country from search text field Select country Selected search Country did not reflect.

    also, App crash when Dismiss Search

    What did you expect to happen?

    Selected search country should appear in the text field The app should not crash on dismiss

    Crash Logs:

    2018-06-13 16:19:54.450526+0530 PocketRehab[2300:25819] [Common] _BSMachError: port 1e53b; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND" 2018-06-13 16:19:54.506719+0530 PocketRehab[2300:25819] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is <UISearchController: 0x7feea6635a00>.' *** First throw call stack: ( 0 CoreFoundation 0x0000000112f941e6 __exceptionPreprocess + 294 1 libobjc.A.dylib 0x0000000112625031 objc_exception_throw + 48 2 UIKit 0x0000000110021a95 __78-[UIViewController _presentViewController:withAnimationController:completion:]_block_invoke + 0 3 UIKit 0x00000001100242c6 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 99 4 UIKit 0x0000000110024958 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511 5 UIKit 0x000000011002422c -[UIViewController _presentViewController:animated:completion:] + 169 6 UIKit 0x0000000110024570 -[UIViewController presentViewController:animated:completion:] + 154 7 UIKit 0x0000000110c25bd9 __51-[UISearchController _performAutomaticPresentation]_block_invoke + 330 8 UIKit 0x0000000110bc7a29 -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 294 9 UIKit 0x0000000110bc36d5 -[_UIViewControllerTransitionContext _runAlongsideCompletions] + 132 10 UIKit 0x0000000110bc33c4 -[_UIViewControllerTransitionContext completeTransition:] + 118 11 UIKit 0x000000010fe754b3 _runAfterCACommitDeferredBlocks + 318 12 UIKit 0x000000010fe6471e _cleanUpAfterCAFlushAndRunDeferredBlocks + 388 13 UIKit 0x000000010fe92ea5 _afterCACommitHandler + 137 14 CoreFoundation 0x0000000112f36607 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 15 CoreFoundation 0x0000000112f3655e __CFRunLoopDoObservers + 430 16 CoreFoundation 0x0000000112f1ab81 __CFRunLoopRun + 1537 17 CoreFoundation 0x0000000112f1a30b CFRunLoopRunSpecific + 635 18 GraphicsServices 0x0000000119403a73 GSEventRunModal + 62 19 UIKit 0x000000010fe6a057 UIApplicationMain + 159 20 PocketRehab 0x000000010f157687 main + 55 21 libdyld.dylib 0x0000000114bc3955 start + 1 22 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

    following link helps me to resolved crash https://stackoverflow.com/questions/46917918/ios-uisearchcontroller-crash-application-tried-to-present-modal-view-controller?rq=1&utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

    bug 
    opened by sujalBandhara 4
  • How can append text programmatically?

    How can append text programmatically?

    General Information

    • NKVPhonePicker Version: Last version
    • iOS Version(s): 11.3.1
    • Swift Version: 4.1
    • Device or Simulator: Device
    • Did you check an example: Yes,

    What happened?

    I am developing a softphone, I have my own numpad. I could not set pressed button value to NKVPhonePickerTextField . It's appending first digit only.

     func buttonPressed(digit: String) {   
        var val = txtDialedNumber.text
        val?.append(digit)
        txtDialedNumber.text = val
    
        // i tried this -> txtDialedNumber.text.append(digit)
     }
    

    What did you expect to happen?

    help wanted 
    opened by ismaiI1 4
  • Force countrycode

    Force countrycode

    General Information

    • NKVPhonePicker Version: 1.2.1
    • iOS Version(s): 11
    • Swift Version: 4
    • Device or Simulator: Device
    • Did you check an example: yes

    What happened?

    there is know way to force country Not to change
    by click on the flag or remove the code text

    What did you expect to happen?

    when delete old mobile it may not delete country code and not change the country flag

    opened by embassem 4
  • SPM - Crash when presenting CountriesViewController

    SPM - Crash when presenting CountriesViewController

    General Information

    • NKVPhonePicker Version: 2.1.1

    • iOS Version(s): iOS 15.5

    • Swift Version: 5.5

    • Device or Simulator: Both

    • Did you check an example:

    What happened?

    1. Upgrading to 2.1.1 from 2.10 using SPM. 2.
    2. Country flag is not set and textfield presents a question mark
    3. Tapping on flag button causes a crash

    Could not find a storyboard named 'CountriesViewController' in bundle NSBundle

    What did you expect to happen?

    Tapping on flag icon should not crash.

    opened by gabemdev 0
  • Dark Mode Support

    Dark Mode Support

    General Information

    • NKVPhonePicker Version:

    • iOS Version(s):

    • Swift Version:

    • Device or Simulator:

    • Did you check an example:

    What happened?

    What did you expect to happen?

    enhancement 
    opened by haarj 1
  • Provide a way to disable the phone formatting calls within the library; formatting functions are broken

    Provide a way to disable the phone formatting calls within the library; formatting functions are broken

    General Information

    • NKVPhonePicker Version:2.1.0

    • iOS Version(s):14.0

    • Swift Version:5.3

    • Device or Simulator:both and no all models

    • Did you check an example: yes

    What happened?

    Let's say you set (either on device or simulator) the flag for chile (using the figer taps, not programatically) and then you type in your number. It's perfect. Now let's say you go back and return to this screen, only this time the request provides you a phone number so you call the preFillText function which calls the setcode, set flag functions and sets the text property. Within both the setflag and setcode functions there are calls for formatting the phone number in accordance to your custom formats (which is not the case here) or to the current country based on the nkv source you pass in using the country code. The sources, the country codes, numbers and all of them are correct but upon "formatting" the number the phone number gets changed and is being added the last 3 chars again in the end of the number. So if the phone number is this (for chile for example): 56 9 2522 5421 then the prefilltext function will mess up with this number and in the end you will see displayed in the phone field this: 56 9 2522 5421421. I managed to overcome this by setting the current country initially to false, setting the text property of the field as is from the response and by manually setting the phonePicker.flagView.flagButton.setImage using the UIImage constructor with the bundle. What would be nice is if we could get acces to the NKVSourcesHelper struct so we can use the static functions from there but i got the job done.

    What did you expect to happen?

    I expected the phone number to not get any other chars added to it, cause that's not ok at all.

    Please provide a boolean so we can set it to false and if that's false then by calling preFillText it won't call the enablePhoneFormat within it and also it won't call the by callin the setFlag function it won't call the textField.presenter.enablePhoneFormat function within it. Please provide access so we can at least call the NKVSourcesHelper struct and just get the flag(for: source) result.

    opened by sorinmiroiu97 0
  • Plus Symbol is not centre for country code

    Plus Symbol is not centre for country code

    General Information

    Hope you find here Screen Short : https://prnt.sc/pxwunv

    • NKVPhonePicker Version: 2.1.10
    • iOS Version(s): 12.4.2
    • Swift Version: 4
    • Device or Simulator: Device iPhone 6
    • Did you check an example: Yes i checked example

    What happened?

    I have textfield with class of NKVPhonePicker and position is (20, 50, 335, 50), I set up Initial country code as IN ...Here is my code /* countryTF?.favoriteCountriesLocaleIdentifiers = ["RU", "ER", "JM"] countryTF?.rightToLeftOrientation = false countryTF?.shouldScrollToSelectedCountry = false countryTF?.flagSize = CGSize(width: 30, height: 50) countryTF?.enablePlusPrefix = true let country = Country.country(for: NKVSource(countryCode: "IN")) countryTF?.country = country countryTF?.customPhoneFormats = ["IN" : "## #### ### ###"] */

    • text is not centre to country code

    What did you expect to happen?

    If + will centre to Country code it looks good . May be it will helpful to all

    bug 
    opened by Raviteja-Mathangi 1
  • How can I use UITextFiledDelegate Methods like Should change characters with nkvtextfields

    How can I use UITextFiledDelegate Methods like Should change characters with nkvtextfields

    General Information

    • NKVPhonePicker Version:

    • iOS Version(s):

    • Swift Version:

    • Device or Simulator:

    • Did you check an example:

    What happened?

    What did you expect to happen?

    help wanted question Сlarification Needed 
    opened by Piyush-Sharma-Pandit 1
  • Same country codes change conuntry flag

    Same country codes change conuntry flag

    General Information

    • NKVPhonePicker Version:

    • iOS Version(s):

    • Swift Version:

    • Device or Simulator:

    • Did you check an example:

    What happened?

    What did you expect to happen?

    Сlarification Needed 
    opened by Quailcreek 2
Releases(2.1.1)
  • 2.1.1(Sep 9, 2021)

    • Increased possible length for code check to 7, i.e. (country code + area code)

    • Added area codes for UK, Guernsey, Isle of Man and Jersey. Source: http://www.prokerala.com Thx @VineetKG

    • Added SPM. Thx @behzadardehei

    • Migrated to swift 5.0 Thx @shahbaz-saleem

    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Jun 26, 2019)

    Added the prefill method:

    let source = NKVSource(countryCode: "ru")
    textField.setCurrentCountryInitially = false
    textField.preFillText(source: source, number: 7999432423)
    

    Thanks @shahbaz-saleem for pushing me to update to swift 5.0 😁 Thanks @artemy-ant for some features 🙌

    Source code(tar.gz)
    Source code(zip)
  • 2.0.3(Dec 18, 2018)

    • Swift version update to 4.2 (thx to @saroar) ⬆
    • Fixed duplicated file and ? icon name (@ARGAMX) 👏🏻
    • Added 2 methods for choosing the correct country if they have the same phone extension. (@Saikedo) ✔️

    Thanks for contributing!) 🔥

    Source code(tar.gz)
    Source code(zip)
  • 2.0.1(Feb 28, 2018)

    This is a major update with totally code refactoring and some new features.

    What's new:

    • Searching with phone extensions, not only with country names;
    • Right to left orientation feature;
    • One model NKVSource for country, countryCode and phone extension;
    • Updated example and gif;
    • Huge code refactoring;
    • Added some patterns to the countries bundle;
    • Fully comments coverage.

    Usage

    ...
    textField.rightToLeftOrientation = true
    

    Please, open an issue if need additional functional!

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Oct 17, 2017)

  • 1.1.0(Aug 26, 2017)

    What's new?

    • Added possibility to choose a phone format patterns (# ### ### ## ##)
    • Removed unnecessary getters
    • Plus is now a separate UILabel
    • Fixed minor and major bugs
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(May 26, 2017)

Owner
Nike Kov
iOS developer. I love people and life!
Nike Kov
CHIOTPField is a set of textfields that can be used for One-time passwords, SMS codes, PIN codes, etc. Mady by @ChiliLabs - https://chililabs.io

CHIOTPField CHIOTPField is a set of textfields that can be used for One-time passwords, SMS codes, PIN codes, etc. All fields support insertion of one

Chili Labs 241 Dec 13, 2022
Animated Subclass of UITextField created with CABasicAnimation and CAShapeLayer

JDAnimatedTextField JDAnimatedTextField is animateable UITextField that can significantly enhance your user's experiences and set your app apart from

Jawad Ali 25 Dec 13, 2022
UITextField subclass with floating labels

JVFloatLabeledTextField JVFloatLabeledTextField is the first implementation of a UX pattern that has come to be known the "Float Label Pattern". Due t

Jared Verdi 7.2k Jan 2, 2023
iOS - Subclass of UITextField to achieve autocompletion for Place Search like Google Places, Uber and Much more apps having maps.

MVAutocompletePlaceSearchTextField iOS - Subclass of UITextField to achieve autocompletion for Place Search like Google Places, Uber and Much more app

Mrugrajsinh Vansadia 68 May 27, 2022
Subclass of UITextField that shows inline suggestions while typing.

AutocompleteField Subclass of UITextField that shows inline suggestions while typing. Plug and play replacement for UITextField. Delimiter support. Pe

Filip Stefansson 663 Dec 6, 2022
UITextfield subclass with autocomplete menu. For iOS.

MLPAutoCompleteTextField "We believe that every tap a user makes drains a tiny bit of their energy and patience. Typing is one of the biggest expendit

Eddy Borja 1.2k Nov 11, 2022
UITextField subclass with autocompletion suggestions list

SearchTextField Overview SearchTextField is a subclass of UITextField, written in Swift that makes really easy the ability to show an autocomplete sug

Alejandro Pasccon 1.1k Dec 28, 2022
TTextField is developed to help developers can initiate a fully standard textfield including title, placeholder and error message in fast and convinient way without having to write many lines of codes

TTextField is developed to help developers can initiate a fully standard textfield including title, placeholder and error message in fast and convinient way without having to write many lines of codes

Nguyen Duc Thinh 7 Aug 28, 2022
UITextField extension in Swift that adds shake animation

UITextField-Shake-Swift UITextField extension in Swift that adds shake animation Initially created by Andrea Mazzini (using Objective-C) on 08/02/14:

null 15 Jul 20, 2021
HTYTextField A UITextField with bouncy placeholder.

HTYTextField - A UITextField with bouncy placeholder. Screenshot Installation CocoaPods Add the dependency to your Podfile

Hanton Yang 312 Nov 13, 2022
Fully-wrapped UITextField made to work entirely in SwiftUI

iTextField ⌨️ A fully-wrapped `UITextField` that works entirely in SwiftUI. ?? Get Started | Examples | Customize | Install | Get Started Install iTex

Benjamin Sage 89 Jan 2, 2023
UITextField character counter with lovable UX 💖. No math skills required 🙃.

TextFieldCounter UITextField character counter with lovable UX ??. No math skills required ??. Features Set max length of UITextField. A beautiful an

Fabricio Serralvo 434 Dec 22, 2022
🏄‍♂️ UITextField-Navigation makes it easier to navigate between UITextFields and UITextViews

' __________________ _______ _________ _______ _________ _______ _ ______ ' |\ /|\__ __/\__ __/( ____ \|\ /

Thanh Pham 446 Nov 24, 2022
Animated UITextField enhance UX for the user by giving clarity that they are focused

JDCircularProgress JDTextField is animateable UITextField that can significantly enhance your user's experiences and set your app apart from the rest

Jawad Ali 22 Nov 17, 2022
This project will add a done button on your UITextField and UITextView

This project will add a done button on your UITextField and UITextView

Botla Venkatesh 0 Nov 23, 2021
UITextField category that adds shake animation

UITextField category that adds a shake animation like the password field of the OsX login screen. Screenshot Setup with CocoaPods pod 'UITextField+Sha

Andrea Mazzini 749 Dec 13, 2022
UITextField with underline and left image

TJTextField UITextField with underline and left image Version: 1.0 Features Add image in UITextField Left text pedding Underline whole UITextField Sho

Tejas Ardeshna 44 May 16, 2022
UITextField and UITextView subclasses with placeholders that change into floating labels when the fields are populated with text.

Deprecated Please use JVFloatLabeledTextField instead or feel free to chime in on an issue if you'd like to take over the repo. RPFloatingPlaceholders

rob phillips 1.1k Jan 5, 2023
UITextField that automatically formats text to display in the currency format

CurrencyTextField The numbers that the user enters in the field are automatically formatted to display in the dollar amount format. For example, if th

Richa Deshmukh 49 Sep 28, 2022