How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?
Asked Answered
A

1

20

I'm playing around with emojis in Swift using Xcode playground for some simple iOS8 apps. For this, I want to create something similar to a unicode/emoji map/description.

In order to do this, I need to have a loop that would allow me to print out a list of emojis. I was thinking of something along these lines

for i in 0x1F601 - 0x1F64F {
    var hex = String(format:"%2X", i)
    println("\u{\(hex)}") //Is there another way to create UTF8 string corresponding to emoji
}

But the println() throws an error

Expected '}'in \u{...} escape sequence. 

Is there a simple way to do this which I am missing?

I understand that not all entries will correspond to an emoji. Also, I'm able create a lookup table with reference from http://apps.timwhitlock.info/emoji/tables/unicode, but I would like a lazy/easy method of achieving the same.

Arronarrondissement answered 2/10, 2014 at 22:51 Comment(2)
I recently wrote a gist available here which gets all standard unicode emojis from the website, parses them and prints them. Also it prints out all Country code emojis from AA to ZZ. I know you don't want to use networking for that but I guess somebody else could use this. Also you could modify it so that it creates a list of numbers of the emojis which can then be used offline โ€“ Tetrafluoroethylene
check this answer https://mcmap.net/q/67028/-how-could-i-get-an-array-of-emojis-found-in-ios8-3 โ€“ Kofu
S
53

You can loop over those hex values with a Range: 0x1F601...0x1F64F and then create the Strings using a UnicodeScalar:

for i in 0x1F601...0x1F64F {
    guard let scalar = UnicodeScalar(i) else { continue }
    let c = String(scalar)
    print(c)
}

Outputs:

๐Ÿ˜๐Ÿ˜‚๐Ÿ˜ƒ๐Ÿ˜„๐Ÿ˜…๐Ÿ˜†๐Ÿ˜‡๐Ÿ˜ˆ๐Ÿ˜‰๐Ÿ˜Š๐Ÿ˜‹๐Ÿ˜Œ๐Ÿ˜๐Ÿ˜Ž๐Ÿ˜๐Ÿ˜๐Ÿ˜‘๐Ÿ˜’๐Ÿ˜“๐Ÿ˜”๐Ÿ˜•๐Ÿ˜–๐Ÿ˜—๐Ÿ˜˜๐Ÿ˜™๐Ÿ˜š๐Ÿ˜›๐Ÿ˜œ๐Ÿ˜๐Ÿ˜ž๐Ÿ˜Ÿ๐Ÿ˜ ๐Ÿ˜ก๐Ÿ˜ข๐Ÿ˜ฃ๐Ÿ˜ค๐Ÿ˜ฅ๐Ÿ˜ฆ๐Ÿ˜ง๐Ÿ˜จ๐Ÿ˜ฉ๐Ÿ˜ช๐Ÿ˜ซ๐Ÿ˜ฌ๐Ÿ˜ญ๐Ÿ˜ฎ๐Ÿ˜ฏ๐Ÿ˜ฐ๐Ÿ˜ฑ๐Ÿ˜ฒ๐Ÿ˜ณ๐Ÿ˜ด๐Ÿ˜ต๐Ÿ˜ถ๐Ÿ˜ท๐Ÿ˜ธ๐Ÿ˜น๐Ÿ˜บ๐Ÿ˜ป๐Ÿ˜ผ๐Ÿ˜ฝ๐Ÿ˜พ๐Ÿ˜ฟ๐Ÿ™€๐Ÿ™๐Ÿ™‚๐Ÿ™ƒ๐Ÿ™„๐Ÿ™…๐Ÿ™†๐Ÿ™‡๐Ÿ™ˆ๐Ÿ™‰๐Ÿ™Š๐Ÿ™‹๐Ÿ™Œ๐Ÿ™๐Ÿ™Ž๐Ÿ™

If you want all the emoji, just add another loop over an array of ranges:

// NOTE: These ranges are still just a subset of all the emoji characters;
//       they seem to be all over the place...
let emojiRanges = [
    0x1F601...0x1F64F,
    0x2702...0x27B0,
    0x1F680...0x1F6C0,
    0x1F170...0x1F251
]

for range in emojiRanges {
    for i in range {
        guard let scalar = UnicodeScalar(i) else { continue }
        let c = String(scalar)
        print(c)
    }
}

For those asking, the full list of available emojis can be found here: https://www.unicode.org/emoji/charts/full-emoji-list.html

A parsable list of unicode sequences for all emojis can be found in the emoji-sequences.txt file under the directory for the version you're interested in here: http://unicode.org/Public/emoji/

As of 9/15/2021 the latest version of the emoji standard available on Apple devices is 13.1.

Simferopol answered 2/10, 2014 at 23:55 Comment(6)
+1 This is so much better than the weird CFStringTransform examples I was playing around with. โ€“ Shumaker
Great solution @Mike. This makes it so simple yet beautiful. โ€“ Arronarrondissement
This is great. But where did you find those ranges for the unicode code points? Do these ranges cover all emoji? As of when? โ€“ Infundibuliform
@Infundibuliform I honestly don't remember where I dug those up, but I think I just used the link in the question and grabbed a few ranges from there. As the note in the code says though, that is definitely not a complete list of all the emoji; it's just there to show one way of looping over a set of ranges. โ€“ Simferopol
If creating a custom keyboard, I'm assuming it isn't against any policies to use these emojis? โ€“ Uraeus
I'm curious how you discovered this. How did you find out which hex values were emojis? โ€“ Formidable

© 2022 - 2024 โ€” McMap. All rights reserved.