Replace iOS app emoji with twitter open source twemoji

前端 未结 2 1023
孤城傲影
孤城傲影 2020-12-10 20:48

I want to replace all standard iOS emoji from a UILable or UITextView with twitters open source twemoji.

I can\'t find any library or documentation to do this in iOS

2条回答
  •  甜味超标
    2020-12-10 21:26

    The question got me intrigued, and after a bit of searching on how it would be possible to replace all standard iOS emoji with a custom set, I noticed that even Twitter's own iOS app doesn't use Twemoji:

    In the end, I came to the same conclusion as you:

    I can't find any library or documentation to do this in iOS.

    So, I created a framework in Swift for this exact purpose.

    It does all the work for you, but if you want to implement your own solution, I'll describe below how to replace all standard emoji with Twemoji.


    1. Document all characters that can be represented as emoji

    There are 1126 base characters that have emoji representations, and over a thousand additional representations formed by sequences. Although most base characters are confined to six Unicode blocks, all but one of these blocks are mixed with non-emoji characters and/or unassigned code points. The remaining base characters outside these blocks are scattered across various other blocks.

    My implementation simply declares the UTF-32 code points for these characters, as the value property of UnicodeScalar is exactly this.

    2. Check whether a character is an emoji

    In Swift, a String contains a collection of Character objects, each of which represent a single extended grapheme cluster. An extended grapheme cluster is a sequence of Unicode scalars that together represent one1 human-readable character, which is helpful since you can loop through the Characters of a string and handling them based on the UnicodeScalars they contain (rather than looping through the UTF-16 values of the string).

    To identify whether a Character is an emoji, only the first UnicodeScalar is significant, so comparing this value to your table of emoji characters is enough. However, I'd also recommend checking if the Character contains a Variation Selector, and if it does, make sure that it's VS16 – otherwise the character shouldn't be presented as emoji.

    Extracting the UnicodeScalars from a Character requires a tiny hack:

    let c: Character = "A"
    let scalars = String(c).unicodeScalars
    

    3. Convert the code points into the correct format

    Twemoji images are named according to their corresponding code points2, which makes sense. So, the next step is to convert the Character into a string equivalent to the image name:

    let codePoint = String("

提交回复
热议问题