š“ó §ó ¢ó „ó ®ó §ó æ Flag: England.
After all, Apple's iPhone only included 10 country flag emojis in 2008, and now there are 268.
This Taiwanese flag is hidden from the emoji keyboard on iOS devices when the region is set to China mainland or (as of iOS 13.1.
Here's a general formula for turning a two-letter country code into its emoji flag:
func flag(country:String) -> String {
let base = 127397
var usv = String.UnicodeScalarView()
for i in country.utf16 {
usv.append(UnicodeScalar(base + Int(i)))
}
return String(usv)
}
let s = flag("DE")
EDIT Ooops, no need to pass through the nested String.UnicodeScalarView struct. It turns out that String has an append
method for precisely this purpose. So:
func flag(country:String) -> String {
let base : UInt32 = 127397
var s = ""
for v in country.unicodeScalars {
s.append(UnicodeScalar(base + v.value))
}
return s
}
EDIT Oooops again, in Swift 3 they took away the ability to append a UnicodeScalar to a String, and they made the UnicodeScalar initializer failable (Xcode 8 seed 6), so now it looks like this:
func flag(country:String) -> String {
let base : UInt32 = 127397
var s = ""
for v in country.unicodeScalars {
s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
}
return String(s)
}
If anyone looking for solution in ObjectiveC here is convenient category:
@interface NSLocale (RREmoji)
+ (NSString *)emojiFlagForISOCountryCode:(NSString *)countryCode;
@end
@implementation NSLocale (RREmoji)
+ (NSString *)emojiFlagForISOCountryCode:(NSString *)countryCode {
NSAssert(countryCode.length == 2, @"Expecting ISO country code");
int base = 127462 -65;
wchar_t bytes[2] = {
base +[countryCode characterAtIndex:0],
base +[countryCode characterAtIndex:1]
};
return [[NSString alloc] initWithBytes:bytes
length:countryCode.length *sizeof(wchar_t)
encoding:NSUTF32LittleEndianStringEncoding];
}
@end
test:
for ( NSString *countryCode in [NSLocale ISOCountryCodes] ) {
NSLog(@"%@ - %@", [NSLocale emojiFlagForISOCountryCode:countryCode], countryCode);
}
output: š¦š© - AD š¦šŖ - AE š¦š« - AF š¦š¬ - AG š¦š® - AI ...
Two optimizations of matt's answer.
Here is the code.
func flag(from country:String) -> String {
let base : UInt32 = 127397
var s = ""
for v in country.uppercased().unicodeScalars {
s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
}
return s
}
I know this is not exactly what was asked, but maybe it will help someone:
let flags: [String: String] = [
"AD": "š¦š©", "AE": "š¦šŖ", "AF": "š¦š«", "AG": "š¦š¬", "AI": "š¦š®", "AL": "š¦š±", "AM": "š¦š²", "AO": "š¦š“", "AQ": "š¦š¶", "AR": "š¦š·", "AS": "š¦šø", "AT": "š¦š¹", "AU": "š¦šŗ", "AW": "š¦š¼", "AX": "š¦š½", "AZ": "š¦šæ", "BA": "š§š¦", "BB": "š§š§", "BD": "š§š©", "BE": "š§šŖ", "BF": "š§š«", "BG": "š§š¬", "BH": "š§š", "BI": "š§š®", "BJ": "š§šÆ", "BL": "š§š±", "BM": "š§š²", "BN": "š§š³", "BO": "š§š“", "BQ": "š§š¶", "BR": "š§š·", "BS": "š§šø", "BT": "š§š¹", "BV": "š§š»", "BW": "š§š¼", "BY": "š§š¾", "BZ": "š§šæ", "CA": "šØš¦", "CC": "šØšØ", "CD": "šØš©", "CF": "šØš«", "CG": "šØš¬", "CH": "šØš", "CI": "šØš®", "CK": "šØš°", "CL": "šØš±", "CM": "šØš²", "CN": "šØš³", "CO": "šØš“", "CR": "šØš·", "CU": "šØšŗ", "CV": "šØš»", "CW": "šØš¼", "CX": "šØš½", "CY": "šØš¾", "CZ": "šØšæ", "DE": "š©šŖ", "DJ": "š©šÆ", "DK": "š©š°", "DM": "š©š²", "DO": "š©š“", "DZ": "š©šæ", "EC": "šŖšØ", "EE": "šŖšŖ", "EG": "šŖš¬", "EH": "šŖš", "ER": "šŖš·", "ES": "šŖšø", "ET": "šŖš¹", "FI": "š«š®", "FJ": "š«šÆ", "FK": "š«š°", "FM": "š«š²", "FO": "š«š“", "FR": "š«š·", "GA": "š¬š¦", "GB": "š¬š§", "GD": "š¬š©", "GE": "š¬šŖ", "GF": "š¬š«", "GG": "š¬š¬", "GH": "š¬š", "GI": "š¬š®", "GL": "š¬š±", "GM": "š¬š²", "GN": "š¬š³", "GP": "š¬šµ", "GQ": "š¬š¶", "GR": "š¬š·", "GS": "š¬šø", "GT": "š¬š¹", "GU": "š¬šŗ", "GW": "š¬š¼", "GY": "š¬š¾", "HK": "šš°", "HM": "šš²", "HN": "šš³", "HR": "šš·", "HT": "šš¹", "HU": "ššŗ", "ID": "š®š©", "IE": "š®šŖ", "IL": "š®š±", "IM": "š®š²", "IN": "š®š³", "IO": "š®š“", "IQ": "š®š¶", "IR": "š®š·", "IS": "š®šø", "IT": "š®š¹", "JE": "šÆšŖ", "JM": "šÆš²", "JO": "šÆš“", "JP": "šÆšµ", "KE": "š°šŖ", "KG": "š°š¬", "KH": "š°š", "KI": "š°š®", "KM": "š°š²", "KN": "š°š³", "KP": "š°šµ", "KR": "š°š·", "KW": "š°š¼", "KY": "š°š¾", "KZ": "š°šæ", "LA": "š±š¦", "LB": "š±š§", "LC": "š±šØ", "LI": "š±š®", "LK": "š±š°", "LR": "š±š·", "LS": "š±šø", "LT": "š±š¹", "LU": "š±šŗ", "LV": "š±š»", "LY": "š±š¾", "MA": "š²š¦", "MC": "š²šØ", "MD": "š²š©", "ME": "š²šŖ", "MF": "š²š«", "MG": "š²š¬", "MH": "š²š", "MK": "š²š°", "ML": "š²š±", "MM": "š²š²", "MN": "š²š³", "MO": "š²š“", "MP": "š²šµ", "MQ": "š²š¶", "MR": "š²š·", "MS": "š²šø", "MT": "š²š¹", "MU": "š²šŗ", "MV": "š²š»", "MW": "š²š¼", "MX": "š²š½", "MY": "š²š¾", "MZ": "š²šæ", "NA": "š³š¦", "NC": "š³šØ", "NE": "š³šŖ", "NF": "š³š«", "NG": "š³š¬", "NI": "š³š®", "NL": "š³š±", "NO": "š³š“", "NP": "š³šµ", "NR": "š³š·", "NU": "š³šŗ", "NZ": "š³šæ", "OM": "š“š²", "PA": "šµš¦", "PE": "šµšŖ", "PF": "šµš«", "PG": "šµš¬", "PH": "šµš", "PK": "šµš°", "PL": "šµš±", "PM": "šµš²", "PN": "šµš³", "PR": "šµš·", "PS": "šµšø", "PT": "šµš¹", "PW": "šµš¼", "PY": "šµš¾", "QA": "š¶š¦", "RE": "š·šŖ", "RO": "š·š“", "RS": "š·šø", "RU": "š·šŗ", "RW": "š·š¼", "SA": "šøš¦", "SB": "šøš§", "SC": "šøšØ", "SD": "šøš©", "SE": "šøšŖ", "SG": "šøš¬", "SH": "šøš", "SI": "šøš®", "SJ": "šøšÆ", "SK": "šøš°", "SL": "šøš±", "SM": "šøš²", "SN": "šøš³", "SO": "šøš“", "SR": "šøš·", "SS": "šøšø", "ST": "šøš¹", "SV": "šøš»", "SX": "šøš½", "SY": "šøš¾", "SZ": "šøšæ", "TC": "š¹šØ", "TD": "š¹š©", "TF": "š¹š«", "TG": "š¹š¬", "TH": "š¹š", "TJ": "š¹šÆ", "TK": "š¹š°", "TL": "š¹š±", "TM": "š¹š²", "TN": "š¹š³", "TO": "š¹š“", "TR": "š¹š·", "TT": "š¹š¹", "TV": "š¹š»", "TW": "š¹š¼", "TZ": "š¹šæ", "UA": "šŗš¦", "UG": "šŗš¬", "UM": "šŗš²", "US": "šŗšø", "UY": "šŗš¾", "UZ": "šŗšæ", "VA": "š»š¦", "VC": "š»šØ", "VE": "š»šŖ", "VG": "š»š¬", "VI": "š»š®", "VN": "š»š³", "VU": "š»šŗ", "WF": "š¼š«", "WS": "š¼šø", "YE": "š¾šŖ", "YT": "š¾š¹", "ZA": "šæš¦", "ZM": "šæš²", "ZW": "šæš¼"
]
Another function for turning a two-letter country code into its emoji flag using Swift 5.
internal func getFlag(from countryCode: String) -> String {
countryCode
.unicodeScalars
.map({ 127397 + $0.value })
.compactMap(UnicodeScalar.init)
.map(String.init)
.joined()
}
To give more insight into matt answer
Swift 2 version
public static func flag(countryCode: String) -> Character {
let base = UnicodeScalar("š¦").value - UnicodeScalar("A").value
let string = countryCode.uppercaseString.unicodeScalars.reduce("") {
var string = $0
string.append(UnicodeScalar(base + $1.value))
return string
}
return Character(string)
}
Swift 3 version, taken from https://github.com/onmyway133/Smile/blob/master/Sources/Smile.swift#L52
public func emoji(countryCode: String) -> Character {
let base = UnicodeScalar("š¦").value - UnicodeScalar("A").value
var string = ""
countryCode.uppercased().unicodeScalars.forEach {
if let scala = UnicodeScalar(base + $0.value) {
string.append(String(describing: scala))
}
}
return Character(string)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With