【问题标题】:JavaScript Cross platform emojis in iOS and androidiOS 和 android 中的 JavaScript 跨平台表情符号
【发布时间】:2017-08-18 00:44:27
【问题描述】:

在 Android 中获取普通表情符号(黄色,图片中的第一个)的最佳方式是什么。如果表情符号可以带有不同的颜色(iOS 表情符号)。原因是 Android 没有彩色表情符号。

android 中的所有表情符号(除第一个之外)都显示“?”象征。

我需要在 JavaScript 中解析表情符号,但我没有找到任何好的库。也许有一个替换功能可以解决这个问题。

【问题讨论】:

  • 我想要的是将彩色表情符号转换为黄色,因为在 iOS 中我可以看到彩色而在 android 中不能(不支持彩色表情符号),所以解决方法是看黄色的。

标签: javascript android ios node.js react-native


【解决方案1】:

表情符号

首先让我们了解什么是表情符号。表情符号是许多字符的组合,因为它以 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, "");

在不了解其背后的概念的情况下,它的工作速度更快,但并不完全清楚它为什么工作。

查看实际操作:https://jsfiddle.net/DerekL/sn4n689r/

【讨论】:

  • 多么出色的答案和细分,希望这个答案获得更多的支持!
猜你喜欢
  • 2019-08-31
  • 2013-02-18
  • 2015-06-28
  • 1970-01-01
  • 2013-08-10
  • 2014-12-10
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
相关资源
最近更新 更多