我正在尝试组装一个可以随机向用户返回表情符号的小型 Android 应用程序。我的 Intent 是只使用实际的 unicode 表情符号字符,并将它们作为 unicode 字符串字符返回。
我构建了一个完整的 unicode 字符串数组,可以从中随机选择,而且很多都可以正确显示。然而,有些显示为不受支持的字符(一个带有 x 的矩形)。
显然不是每个平台都支持每个 unicode 表情符号字符,但如果可能的话,我想要一种方法来确定什么是受支持的字符,什么不是受支持的字符。理想的情况是查询支持的字符列表,但能够测试单个字符也能很好地完成这项工作。
最佳答案
另请查看 API 级别 23 中添加的 Paint.hasGlyph(String)
。您可以使用它来测试表情符号等字符是否有可用的字形。
这就是documentation说:
boolean hasGlyph (String string)
Determine whether the typeface set on the paint has a glyph supporting the string. The simplest case is when the string contains a single character, in which this method determines whether the font has the character. In the case of multiple characters, the method returns true if there is a single glyph representing the ligature. For example, if the input is a pair of regional indicator symbols, determine whether there is an emoji flag for the pair.
Finally, if the string contains a variation selector, the method only returns true if the fonts contains a glyph specific to that variation.
Checking is done on the entire fallback chain, not just the immediate font referenced.
另见
关于android - 检测是否支持 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162812/