Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emoji skin-tone detect

Tags:

ios

swift

emoji

Following this post I tried to update the code from Swift 2.0 to Swift 5.0 to check which emojis have skin tones available or not and other variations already present. My updated code in detail:

extension String {
var emojiSkinToneModifiers: [String] {
    return [ "🏻", "🏼", "🏽", "🏾", "🏿" ]
}

var emojiVisibleLength: Int {
    var count = 0
    enumerateSubstrings(in: startIndex..<endIndex, options: .byComposedCharacterSequences) { (_, _, _, _) in
        count = count + 1
    }
    return count
}

var emojiUnmodified: String {
    if self.count == 0 {
        return ""
    }
    let range = String(self[..<self.index(self.startIndex, offsetBy: 1)])
    return range
}

var canHaveSkinToneModifier: Bool {
    if self.count == 0 {
        return false
    }
    let modified = self.emojiUnmodified + self.emojiSkinToneModifiers[0]
    return modified.emojiVisibleLength == 1
}
}

And use this with an array:

let emojis = [ "πŸ‘", "πŸ‘πŸΏ", "🐸" ]
for emoji in emojis {
   if emoji.canHaveSkinToneModifier {
        let unmodified = emoji.emojiUnmodified
        print(emoji)
        for modifier in emoji.emojiSkinToneModifiers {
            print(unmodified + modifier)
        }
    } else {
      print(emoji)
    }
 }

The output: πŸ‘πŸ‘πŸ»πŸ‘πŸΌπŸ‘πŸ½πŸ‘πŸΎπŸ‘πŸΏ πŸ‘πŸΏπŸ‘πŸΏπŸ»πŸ‘πŸΏπŸΌπŸ‘πŸΏπŸ½πŸ‘πŸΏπŸΎπŸ‘πŸΏπŸΏ 🐸🐸🏻🐸🏼🐸🏽🐸🏾🐸🏿 assigns variations to emojis that do not have them or that already is instead of: πŸ‘πŸ‘πŸ»πŸ‘πŸΌπŸ‘πŸ½πŸ‘πŸΎπŸ‘πŸΏ πŸ‘πŸΏ 🐸

I suppose enumerateSubstringsInRange is incorrect and self.characters.count now became self.count easy and correct to count one emoji (composed) compared to before Swift 4 but maybe not useful in this case. What am I not seeing wrong? Thanks

like image 983
Joannes Avatar asked Sep 16 '25 23:09

Joannes


1 Answers

A "hack" would be to compare the visual representation of a correct emoji (like "🐸") and a wanna-be emoji (like "🐸🏽").

I've modified your code here and there to make it work:

extension String {
    static let emojiSkinToneModifiers: [String] =  ["🏻", "🏼", "🏽", "🏾", "🏿"]

    var emojiVisibleLength: Int {
        var count = 0
        let nsstr = self as NSString
        let range = NSRange(location: 0, length: nsstr.length)
        nsstr.enumerateSubstrings(in: range,
                            options: .byComposedCharacterSequences)
        { (_, _, _, _) in

            count = count + 1
        }
        return count
    }

    var emojiUnmodified: String {
        if isEmpty {
            return self
        }
        let string = String(self.unicodeScalars.first!)
        return string
    }

    private static let emojiReferenceSize: CGSize = {
        let size = CGSize(width : CGFloat.greatestFiniteMagnitude,
                          height: CGFloat.greatestFiniteMagnitude)
        let rect = ("πŸ‘" as NSString).boundingRect(with: size,
                                                     options: .usesLineFragmentOrigin,
                                                     context: nil)
        return rect.size
    }()

    var canHaveSkinToneModifier: Bool {
        if isEmpty {
            return false
        }

        let modified = self.emojiUnmodified + String.emojiSkinToneModifiers[0]

        let size = (modified as NSString)
            .boundingRect(with: CGSize(width : CGFloat.greatestFiniteMagnitude,
                                       height: .greatestFiniteMagnitude),
                          options: .usesLineFragmentOrigin,
                          context: nil).size

        return size == String.emojiReferenceSize
    }
}

Let's try it out:

let emojis = [ "πŸ‘", "πŸ‘πŸΏ", "🐸" ]
for emoji in emojis {
    if emoji.canHaveSkinToneModifier {
        let unmodified = emoji.emojiUnmodified
        print(unmodified)
        for modifier in String.emojiSkinToneModifiers {
            print(unmodified + modifier)
        }
    } else {
        print(emoji)
    }
    print("\n")
}

And voila!

πŸ‘
πŸ‘πŸ»
πŸ‘πŸΌ
πŸ‘πŸ½
πŸ‘πŸΎ
πŸ‘πŸΏ
πŸ‘
πŸ‘πŸ»
πŸ‘πŸΌ
πŸ‘πŸ½
πŸ‘πŸΎ
πŸ‘πŸΏ
🐸
like image 194
ielyamani Avatar answered Sep 18 '25 18:09

ielyamani