How to separate emojis entered (via default keyboard) in a text box

I entered two emojis in the text box , here I get a total of 5 characters, while 4 characters for the first emoji and 1 character for the second. It looks like the apple combined 4 emojis to form one.

I am looking for quick code where I can separately separate each of emojis, suppose, taking the above example, I should get 2 lines / characters separately for each emoji.

Can anyone help me solve this problem, I have tried many things, such as regex separation or SeparatedByString or characterSet components. but unfortunately ended in negative.

Thanks in advance.

+6
source share
2 answers

Update for Swift 4 (Xcode 9)

As with Swift 4 (tested using Xcode 9 beta), Sequence Emoji ZWJ is treated as one Character , as specified in the Unicode 9 standard:

 let str = "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§πŸ˜" print(str.count) // 2 print(Array(str)) // ["πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§", "😍"] 

String is also its character set (again), so we can call str.count to get the length, and Array(str) to get all the characters as an array.


(Old answer for Swift 3 and earlier)

This is only a partial answer that may help in this particular case.

"πŸ‘¨πŸ‘¨πŸ‘§πŸ‘§" really is a combination of four separate characters:

 let str = "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§πŸ˜" // print(Array(str.characters)) // Output: ["πŸ‘¨β€", "πŸ‘¨β€", "πŸ‘§β€", "πŸ‘§", "😍"] 

which are glued together with U + 200D (ZERO WIDTH JOINER):

 for c in str.unicodeScalars { print(String(c.value, radix: 16)) } /* Output: 1f468 200d 1f468 200d 1f467 200d 1f467 1f60d */ 

Listing a string using the .ByComposedCharacterSequences option correctly combines these characters:

 var chars : [String] = [] str.enumerateSubstringsInRange(str.characters.indices, options: .ByComposedCharacterSequences) { (substring, _, _, _) -> () in chars.append(substring!) } print(chars) // Output: ["πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§", "😍"] 

But there are other cases when this does not work, for example, "flags", which are a sequence of "Regional indicator" symbols "(cf. Swift countElements () returns an invalid value for the count emoji flags ).

 let str = "πŸ‡©πŸ‡ͺ" 

the result of the above loop is

 ["πŸ‡©", "πŸ‡ͺ"] 

which is not the desired result.

Complete rules are defined in β€œ3 Graphic Cluster Borders” in β€œUNICODE TEXT SEGMENTATION Standard Application No. 29” in Unicode Standard.

+3
source

You can use this example code or this pod .

To use it in Swift, import the category into YourProject_Bridging_Header

 #import "NSString+EMOEmoji.h" 

Then you can check the range for each emoji in your line:

 let example: NSString = "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§πŸ˜" // your string let ranges: NSArray = example.emo_emojiRanges() // ranges of the emojis for value in ranges { let range:NSRange = (value as! NSValue).rangeValue print(example.substringWithRange(range)) } // Output: ["πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§", "😍"] 

I created a small sample project with the above code.

For further reading, this is an interesting article from Instagram .

+1
source

All Articles