Code Monkey home page Code Monkey logo

nicklockwood / countrypicker Goto Github PK

View Code? Open in Web Editor NEW
740.0 23.0 153.0 863 KB

CountryPicker is a custom UIPickerView subclass that provides an iOS control allowing a user to select a country from a list. It can optionally display a flag next to each country name, and the library includes a set of 249 high-quality, public domain flag images from FAMFAMFAM (http://www.famfamfam.com/lab/icons/flags/) that have been painstakingly re-named by country code to work with the library.

Home Page: http://charcoaldesign.co.uk/source/cocoa#countrypicker

License: Other

Objective-C 100.00%

countrypicker's Introduction

License CocoaPods CocoaPods Twitter

Screenshot

Purpose

CountryPicker is a custom UIPickerView subclass that provides an iOS control allowing a user to select a country from a list. It can optionally display a flag next to each country name, and the library includes a set of 249 public domain flag images from https://github.com/koppi/iso-country-flags-svg-collection that have been renamed to work with the library.

Note that the list of countries is based on the ISO 3166 country code standard (http://en.wikipedia.org/wiki/ISO_3166-1). This list excludes certain smaller countries, regarding them as part of a larger state. For example, England, Scotland, Wales and Northern Ireland are lumped together as Great Britain. For most purposes this is fine as it matches the convention used for locales, but if you need to specify additional countries, you can subclass and modify the countires list as described under "Subclassing" below.

Supported iOS & SDK Versions

  • Supported build target - iOS 10.0 (Xcode 8.0, Apple LLVM compiler 8.0)
  • Earliest supported deployment target - iOS 6.0
  • Earliest compatible deployment target - iOS 5.0

NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this iOS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.

ARC Compatibility

As of version 1.1, CountryPicker requires ARC. If you wish to use CountryPicker in a non-ARC project, just add the -fobjc-arc compiler flag to the CountryPicker.m file. To do this, go to the Build Phases tab in your target settings, open the Compile Sources group, double-click CountryPicker.m in the list and type -fobjc-arc into the popover.

If you wish to convert your whole project to ARC, comment out the #error line in CountryPicker.m, then run the Edit > Refactor > Convert to Objective-C ARC... tool in Xcode and make sure all files that you wish to use ARC for (including CountryPicker.m) are checked.

Installation

To use the CountryPicker in an app, just drag the CountryPicker class files into your project. If you want to use the flag icons, drag the CountryPicker.bundle folder in also.

Alternatively, you can install CountryPicker using Cocoapods or Carthage,

Subclassing

As of version 1.0.2 you can easily subclass CountryPicker to modify the country name/code list.

To add additional countries, override the +countryNamesByCode method (there is no need to override +countryCodesByName as this is derived automatically from +countryNamesByCode).

To change the display order, or display duplicate copies of (say) US or UK at the top of the list, override +countryNames method (there is no need to override +countryCodes as this is derived automatically from +countryNames and +countryCodesByName).

Release notes

Version 1.3

  • Updated project for Xcode 8
  • You can now override the font used by the picker labels
  • Country sorting is now correct for localized names
  • Flag images are now loaded from the correct bundle
  • Added Carthage support

Version 1.2.3

  • Moved flag images into a resource bundle

Version 1.2.2

  • Added workaround for simulator bug where currentLocale doesn't return country names

Version 1.2.1

  • Updated for iOS 8
  • Now compliant with -Weverything warning level

Version 1.2

  • Removed Countries.plist - country list is now generated automatically
  • Country names are now localized
  • Added ability to set and get country using locale
  • Removed the setWithLocale: method

Version 1.1

  • Updated for iOS 7 compatibility
  • Added new "flat" flag images for iOS 7
  • Now requires ARC (see README for details)
  • Now compliant with -Wall and -Wextra warning levels

Version 1.0.2

  • Capitalized Japan
  • Added South Sudan
  • Refactored to make subclassing easier

Version 1.0.1

  • Added ARC support
  • Added example project

Version 1.0

  • Initial release

countrypicker's People

Contributors

andreyvit avatar erichedstrom avatar hearther avatar nicklockwood avatar soffes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

countrypicker's Issues

Change Localized

I have an app that user can change language. But when i change the language, countrypicker did not. It only change when i close and open again the app. Is there something that i can explicit change the language?

Compile time is very slow

First of all, I want to say this is a VERY nice and easy to use plugin. No matter what! Now, after adding CountryPicker as a pod in my project, my time to compile and launch the simulator went from 3-4 seconds, to 8-10 seconds. I removed it, and went back to 3-4 seconds. Any ideas as to why?!

Thanks!

reloadAllComponents not working

I'm trying to filter the country list modifying the countryNamesByCode, as mentioned by the documentation. But I need to refresh the elements, with a new _countryNamesByCode (which i've modified). When I use the pickerView method reloadAllComponents, the picker don't suffer any modification.
But if I try to do the same logic with the countryCodes method and static variable, i'm able to modify the list but the flags aren't modified...

kind of need some help here...
thanks

Request: Don't localized country name

I need the country name to always be in English. (Disregard localization and i18n)
(Localized country name is a great feature but I would like to have an option to define single permanent language).
Is it possible with current code? Can you make it available?
Great picker - Thanks!

Issue with iOS8

NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];

countryName is always nil in iOS 8 (beta 2)

Wrong country code for KOSOVO = 0

Hi,

Randomly check country code and found a issue(I don't know it's only for me or others too). For country KOSOVO have country code is "0" for all other it's working fine.

Thanks.

How do I get initial information of country without using onSelect?

I have a component that initialises the CountryPicker with a countryCode. Now I want to find out the callingCode of the country without having the user actually select anything.

E.g.

  const [cca2, setCca2] = useState<CountryCode>('AU');

<CountryPicker
          containerButtonStyle={{marginBottom: 10}}
          onSelect={(value) => selectCountry(value)}
          translation="common"
          countryCode={cca2}
        />

How do I get country.callingCode at the start before the user has interacted with the picker?

iOS 8

Flag not showing on iOS 8

Please add imageForCountryCode

I made a small change to your code (CountryPicker.m, v1.3)

starting in line 230 added the function:

-(UIImage*)imageForCountryCode:(NSString*)countryCode
{
    NSString *imagePath = [NSString stringWithFormat:@"CountryPicker.bundle/%@", countryCode];
    UIImage *image;
    if ([[UIImage class] respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)])
    {
        image = [UIImage imageNamed:imagePath inBundle:[NSBundle bundleForClass:[CountryPicker class]] compatibleWithTraitCollection:nil];
    }
    else
    {
        image = [UIImage imageNamed:imagePath];
    }

    return image;
}

So by subclassing, one can add flags for extra added nations. I needed to add nations such as Soviet Union to collect people's country of birth.

Then changed the function at row 230:

- (UIView *)pickerView:(__unused UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(__unused NSInteger)component reusingView:(UIView *)view
{
    if (!view)
    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 30)];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35, 3, 245, 24)];
        label.backgroundColor = [UIColor clearColor];
        label.tag = 1;
        if (self.labelFont)
        {
            label.font = self.labelFont;
        }
        [view addSubview:label];
        
        UIImageView *flagView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 24, 24)];
        flagView.contentMode = UIViewContentModeScaleAspectFit;
        flagView.tag = 2;
        [view addSubview:flagView];
    }

    ((UILabel *)[view viewWithTag:1]).text = [[self class] countryNames][(NSUInteger)row];  

    UIImage *image = [self imageForCountryCode:[[self class] countryCodes][(NSUInteger) row]];

    ((UIImageView *)[view viewWithTag:2]).image = image;
    return view;
}

Of course the imageForCountryCode API needs to be added to the interface.
Could you please insert this change in future updates, or provide some mechanism to add flags to extra added nations?
Thanks, David

Cocoapods integration

Can this be integrated with cocoapods? if it is already, can the readme file installation section mentions it? Thanks in advance (:

Proposal

It's not rare, when you need countries for their phone code number. Is it available now? or will it be supported in the future?

Support for Swift Package Manager

As of SPM arrival with Xcode 11 a lot of project are being migrated from carthage and pods package architectures to Swift Package Manager. Is there any plans to add support for SPM for this library?

Custom country order

Hi Nick,
Thanks for sharing CountryPicker.

I have a question regarding the order of the countries. What if I want to have "United States" as the first country in list, without simply forking your repo (which I did here: https://github.com/instaply/CountryPicker) ?

I tried with subclassing but the subclass doesn't see the static variables :(
And I guess it will be the same for category.

Any suggestion?
Your help will be much appreciated.
Thx,
Arnaud

Cannot display flag image

In Readme you say:
If you want to use the flag icons, drag the CountryPicker.bundle folder in also.
but even if I did it, I cannot see flag. Can you tell me why?
I am working in Version 5.1.1 (5B1008).
Please help me.
Thanks.

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.