Swift turn a country code into a emoji flag via unicode
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)
}
Get emoji flag from country code in Ruby
Use tr
to translate alphabetic characters to their regional indicator symbols:
'US'.tr('A-Z', "\u{1F1E6}-\u{1F1FF}")
#=> "br>
Of course, you can also use the Unicode characters directly:
'US'.tr('A-Z', '-')
#=> "br>
Android Get Country Emoji Flag Using Locale
I was looking for that too but I don't think it's possible yet.
Have a look here:
http://developer.android.com/reference/java/util/Locale.html
No mentioning about flags.
_
Alternately you can check the answer here:
Android Countries list with flags and availability of getting iso mobile codes
that might help you.
How to convert two-letter country codes to flag emojis?
Solution:
public static string IsoCountryCodeToFlagEmoji(this string country)
{
return string.Concat(country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
}
string gb = "gb".IsoCountryCodeToFlagEmoji(); // br>string fr = "fr".IsoCountryCodeToFlagEmoji(); // br>
Get emoji flag by country code
This code snippet worked for me. Just replace "US"
with whichever valid country code (based on the Regional Indicator Symbol Letters) you like and it will create a String flag
containing the flag emoji for that country. (Reference)
int flagOffset = 0x1F1E6;
int asciiOffset = 0x41;
String country = "US";
int firstChar = Character.codePointAt(country, 0) - asciiOffset + flagOffset;
int secondChar = Character.codePointAt(country, 1) - asciiOffset + flagOffset;
String flag = new String(Character.toChars(firstChar))
+ new String(Character.toChars(secondChar));
This answer helped
using regional indicator symbols for languages?
No, that would not be appropriate. Language codes and region codes are wildly different things – any correspondence between them is purely incidental and they don’t always match in obvious ways. For example, the country of Greece has code GR, but the Greek language has code EL.
Regional Indicator sequences as defined by the Unicode Standard only apply to region codes (as their name implies), in particular regions with a two-letter Unicode region subtag, which is based on ISO 3166-1 alpha-2. See Annex B of Unicode Technical Standard #51 for more information. Esperanto has no region code because it is not a region.
EO is currently an unassigned code – it does not belong to any region and thus may be assigned to a new entity at any moment. If you display “” as the Esperanto flag in your font, then anything written in your font will be compromised when a new country or territory is given the code EO in the future because you’ve been using the wrong flag for that code all the time.
Related Topics
Swift Spritekit Adding Button Programmatically
Spritekit Physics in Swift - Ball Slides Against Wall Instead of Reflecting
Check If Key Exists in Dictionary of Type [Type:Type]
Use Reserved Keyword a Enum Case
In Xcode 6.1. 'Uiimage' Does Not Have a Member Named 'Size' Error
How to Stop Timer in Text View
Navigationview Pops Back to Root, Omitting Intermediate View
What Is the Real Benefit of Using Raycast in Arkit and Realitykit
Default Value For Optional Generic Parameter in Swift Function
In Swift 3, How to Calculate the Factorial When the Result Becomes Too High
Function with Datatask Returning a Value
Reasons to Include Function in Protocol Definition VS. Only Defining It in the Extension
Aws Cognito Swift Credentials Provider "Logins Is Deprecated: Use Awsidentityprovidermanager"
How String Comparison Happens in Swift
Propagate an Optional Through a Function (Or Init) in Swift
How to Document the Parameters of a Function'S Closure Parameter in Swift 3