22

The emoji 👍🏼 consists of 2 unicodeScalars 👍 U+1F44D, 🏼 U+1F3FC.

How can this be identified as 1 'displayed' emoji as it will be displayed as such on iOS?

2

1 Answer 1

19

Update for Swift 4 (Xcode 9)

As of Swift 4, a "Emoji sequence" is treated as a single grapheme cluster (according to the Unicode 9 standard):

let s = "a👍🏼b👨‍❤️‍💋‍👨"
print(s.count) // 4

so the other workarounds are not needed anymore.


(Old answer for Swift 3 and earlier:)

A possible option is to enumerate and count the "composed character sequences" in the string:

let s = "a👍🏼b👨‍❤️‍💋‍👨"
var count = 0
s.enumerateSubstringsInRange(s.startIndex..<s.endIndex,
                             options: .ByComposedCharacterSequences) {
                                (char, _, _, _) in
                                if let char = char {
                                    count += 1
                                }
}
print(count) // 4

Another option is to find the range of the composed character sequences at a given index:

let s = "👨‍❤️‍💋‍👨"
if s.rangeOfComposedCharacterSequenceAtIndex(s.startIndex) == s.characters.indices {
    print("This is a single composed character")
}

As String extension methods:

// Swift 2.2:
extension String {
    var composedCharacterCount: Int {
        var count = 0
        enumerateSubstringsInRange(characters.indices, options: .ByComposedCharacterSequences) {
            (_, _, _, _) in count += 1
        }
        return count
    }

    var isSingleComposedCharacter: Bool {
        return rangeOfComposedCharacterSequenceAtIndex(startIndex) == characters.indices
    }
}

// Swift 3:
extension String {
    var composedCharacterCount: Int {
        var count = 0
        enumerateSubstrings(in: startIndex..<endIndex, options: .byComposedCharacterSequences) {
            (_, _, _, _) in count += 1
        }
        return count
    }

    var isSingleComposedCharacter: Bool {
        return rangeOfComposedCharacterSequence(at: startIndex) == startIndex..<endIndex
    }
}

Examples:

"👍🏼".composedCharacterCount // 1
"👍🏼".characters.count       // 2

"👨‍❤️‍💋‍👨".composedCharacterCount // 1
"👨‍❤️‍💋‍👨".characters.count       // 4

"🇩🇪🇨🇦".composedCharacterCount // 2
"🇩🇪🇨🇦".characters.count       // 1

As you see, the number of Swift characters (extended grapheme clusters) can be more or less than the number of composed character sequences.

10
  • This is awesome 👍🏼 How does this work for 👨‍❤️‍👨? It has 6 unicodeScalars, 3 characters, and the rangeOfComposedCharacterSequenceAtIndex(startIndex) is 0..<8.
    – Manuel
    Aug 23, 2016 at 15:37
  • @Manuel: It works also for flags (see added examples) which I find even more surprising.
    – Martin R
    Aug 23, 2016 at 15:40
  • 1
    Shouldn't "🇩🇪🇨🇦".characters.count be 4 instead of 1?
    – Manuel
    Aug 23, 2016 at 15:43
  • 2
    @Manuel: The "regional indicators" are a strange thing, compare stackoverflow.com/questions/26862282/…. Any sequence of Regional_Indicator (RI) characters is considered a single grapheme cluster.
    – Martin R
    Aug 23, 2016 at 15:45
  • 1
    @Manuel: print(Array("👨‍❤️‍👨".unicodeScalars)) might be instructive. There are three Swift characters, but 6 Unicode scalars (including U+200D ZERO-WIDTH JOINER). The Unicode scalars > U+FFFF consume two index positions (they are internally stored as UTF-16 surrogate pair). – Unicode is fun!
    – Martin R
    Aug 23, 2016 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.