Replace iOS app emoji with twitter open source twemoji -


i want replace standard ios emoji uilable or uitextview twitters open source twemoji.

i can't find library or documentation in ios. have solution not involve me implementing scratch?

the solution needs efficient , work offline.

the question got me intrigued, , after bit of searching on how possible replace all standard ios emoji custom set, noticed twitter's own ios app doesn't use twemoji:

tweet

in end, came same conclusion you:

i can't find library or documentation in ios.

so, created a framework in swift exact purpose.

it work you, if want implement own solution, i'll describe below how replace all standard emoji twemoji.


1. document characters can represented emoji

there 1126 base characters have emoji representations, , on thousand additional representations formed sequences. although base characters confined 6 unicode blocks, 1 of these blocks mixed non-emoji characters and/or unassigned code points. remaining base characters outside these blocks scattered across various other blocks.

my implementation declares utf-32 code points these characters, value property of unicodescalar this.

2. check whether character emoji

in swift, string contains collection of character objects, each of represent single extended grapheme cluster. extended grapheme cluster sequence of unicode scalars represent one1 human-readable character, helpful since can loop through characters of string , handling them based on unicodescalars contain (rather looping through utf-16 values of string).

to identify whether character emoji, first unicodescalar significant, comparing value table of emoji characters enough. however, i'd recommend checking if character contains variation selector, , if does, make sure it's vs16 – otherwise character shouldn't presented emoji.

extracting unicodescalars character requires tiny hack:

let c: character = "a" let scalars = string(c).unicodescalars 

3. convert code points correct format

twemoji images named according corresponding code points2, makes sense. so, next step convert character string equivalent image name:

let codepoint = string("🙃").unicodescalars.first!.value  // 128579 let imagename = string(codepoint, radix: 16)              // "1f643" 

great, won't work flags or keycaps, we'll have modify our code take account:

let scalars = string("🇧🇪").unicodescalars let filtered = scalars.filter{ $0.value != 0xfe0f }       // remove vs16 variants, including keycaps. let mapped = filtered.map{ string($0.value, radix: 16) } let imagename = mapped.joined(separator: "-")             // "1f1e7-1f1ea" 

4. replace emoji in string

in order replace emoji in given string, we'll need use nsmutableattributedstring storing original string, , replace emoji nstextattachment objects containing corresponding twemoji image.

let originalstring = "🙃"  let attributedstring = nsmutableattributedstring(string: originalstring)  character in originalstring.characters {     // check if character emoji, see section 2.     ...      // image name character, see section 3.     let imagename = ...      // safely unwrapping make sure image exists.     if let image = uiimage(named: imagename) {         let attachment = nstextattachment()         attachment.image = image          // create attributed string attachment.         let twemoji = nsattributedstring(attachment: attachment)          // range of character in attributedstring.         let range = attributedstring.mutablestring.range(of: string(character))          // replace emoji corresponding twemoji.         attributedstring.replacecharacters(in: range, with: twemoji)     } } 

to display resulting attributed string, set attributedtext property of uitextview/uilabel.

note above method doesn't take account zero-width joiners or modifier sequences, feel answer long stands.


1. there quirk character type interprets sequence of joined regional indicator symbols 1 object, despite containing theoretically unlimited amount of unicode scalars. try "🇩🇰🇫🇮🇮🇸🇳🇴🇸🇪".characters.count in playground.

2. naming pattern varies when comes zero-width joiners , variation selectors, it's easier strip these out of image names – see here.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -