在 Android 中获取正常表情符号(黄色的,图中第一个)的最佳方法是什么。如果表情符号可以带有不同的颜色(iOS 表情符号)。原因是 Android 没有彩色表情符号。
android 中的所有表情符号少第一个显示 '?'象征。
我需要用 JavaScript 解析表情符号,但我没有找到任何好的库。也许有一个替换函数可以解决这个问题。
最佳答案
表情符号
首先让我们了解什么是表情符号。表情符号是许多字符的组合,因为它是在 Unicode 中实现的。比如“浅肤色男耸肩表情”其实就是以下5个字符的组合:
0x1f937 0x1f3fb 0x200d 0x2642 0xfe0f
- 0x1f937: Person Shrugging
- 0x1f3fb: Light skin tone
- 0x200d: Zero width joiner (Used to combine two characters, in this case
the Light skin tone Person shrugging and the
male sign.)
- 0x2642: Male sign
- 0xfe0f: Variation selector-16 (Used to indicate an Emoji)
JavaScript 中的 Unicode
好消息是,我们可以简单地删除 0x1f3fb 以获得没有肤色的版本。坏消息是,JavaScript 不支持 UTF-16,因此显示为
0xd83e 0xdd37 0xd83c 0xdffb 0x200d 0x2642 0xfe0f
└───────── Uh oh ─────────┘
相反,这是不正确的,因为它不知道 surrogate pair 是什么是。要计算正确的代码点,我们必须引用 UTF-16 standard并进行必要的更正。还好,someone else already did the hard work在这里我将字符串转换为正确的 UTF-16 并删除我不想要的部分:
// Defines the range of skin tone modifiers
var modifiersMin = 0x1F3FB, modifiersMax = 0x1F3FF;
// Emoji with U+1F3FB "light skin tone" modifier combined with "male sign"
var string = new UnicodeString("🤷🏻♂️");
// Removes the modifier
string = new UnicodeString(string.codePoints.filter(c => {
return c < modifiersMin || c > modifiersMax;
});
alert(string.toString());
您可以在此处查看实际效果:https://jsfiddle.net/DerekL/b5848tor/
现在您了解了表情符号的工作原理,您还可以这样做:
// The replace function that you would like
var skinToneModifiers = new RegExp("\ud83c[\udffb-\udfff]", "g");
var string = "🤷🏻♂️";
// Remove skin tone modifier
string = string.replace(skinToneModifiers, "");
它工作得更快,但不完全清楚为什么它在不理解其背后的概念的情况下工作。
关于iOS 和安卓中的 JavaScript 跨平台表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746872/