FlagsKit is a swift package that contains more than 200 country flag png
images. All the images are in low resulution (most of them are 250 x 125) what makes them together have a weight that less than 750 KB. Package provides a very simple way of getting a SwiftUI
view with the flag you need. You can use it when you need a small image of the flag like in example below.
It also has mechanisms of converting Phone Code or Currency Code to Country code and getting Emoji flags based on Country, Phone or Currency code.
It's a Swift Package, so you need to do the following:
- Open
File
in Xcode menu - Open
Swift Packages
- Choose
Add Package Dependency...
option - In the
Enter package repository URL
field paste this URL:https://github.com/pichukov/FlagsKit
- Choose any existing version or take it from
main
branch
Add import FlagsKit
to your swift file.
By using Country
enum you will be able to get a country code or Emoji flag of the country from Currency or Phone code.
The following code example will print you CZ
country code
let countryCode = Country.countryCode(fromPhoneCode: "+420")
print(countryCode)
If you need to get a country code from Currency code, you can do it like this:
let countryCode = Country.countryCode(fromCurrencyCode: "USD")
print(countryCode)
In that case US
will be prented
To get an Emoji flag you can use one of the followint methods:
Country.flagEmoji(forCountryCode: "US") // -> ๐บ๐ธ
Country.flagEmoji(forCurrencyCode: "USD") // -> ๐บ๐ธ
Country.flagEmoji(forPhoneCode: "+420") // -> ๐จ๐ฟ
If you need a SwiftUI
view with country flag, use the following code:
FlagView(countryCode: "US")
If you need a circle flag or the one with rounded corners, use style
parameter:
FlagView(countryCode: "ro", style: .circle)
.frame(width: 50, height: 50)
In this example you will have a circle 50 by 50 flag.