ios - 检测何时无法正确显示 unicode 字符

标签 ios swift unicode watchkit

部分unicode字符在iOS上无法显示,但在macOS上可以正常显示。同样,iOS 可以显示的一些 unicode 字符在 watchOS 上无法显示。这是由于这些平台上安装了不同的内置字体。

当无法显示字符时,它显示为 ?在一个盒子里,像这样:
enter image description here

我还看到一些角色显示为外星人(不确定为什么不同):
enter image description here

有没有办法知道给定一个 unicode 字符字符串(例如 "ᄥ"),何时无法正确显示特定的 unicode 字符?

我需要一个适用于 iOS 和 watchOS 的解决方案。

最佳答案

您可以使用 CTFontGetGlyphsForCharacters() 来确定字体是否具有特定代码点的字形(请注意补充字符需要作为代理项对进行检查):

CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), 12, NULL);
const UniChar code_point[] = { 0xD83C, 0xDCA1 };  // U+1F0A1
CGGlyph glyph[] = { 0, 0 };
bool has_glyph = CTFontGetGlyphsForCharacters(font, code_point, glyph, 2);

或者,在 Swift 中:

let font = CTFontCreateWithName("Helvetica", 12, nil)
var code_point: [UniChar] = [0xD83C, 0xDCA1]
var glyphs: [CGGlyph] = [0, 0]
let has_glyph = CTFontGetGlyphsForCharacters(font, &code_point, &glyph, 2)

如果您想检查系统将尝试从中加载字形的完整后备字体集,您将需要检查 CTFontCopyDefaultCascadeListForLanguages() 返回的所有字体。检查answer to this question有关如何创建后备字体列表的信息。

关于ios - 检测何时无法正确显示 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363211/

相关文章:

ios - App Store 上的我的应用程序在未发布更新的情况下自行更新

ios - 如何从 Kinvey 获取自定义属性

ios - 如何合并两个仅包含唯一值的字典

ios - 如何使用 Swift 将 "Done"按钮添加到 iOS 中的小键盘?

ios - SQLite 中的越南语 Unicode 文本搜索

unicode - Unicode 字符如何映射到字体中的字形?

ios - 如何避免对加密 key 进行硬编码( Objective-C )?

ios - 将 AdMob 添加到 Firebase 项目时崩溃

ios - 将短信分享到 Instagram Direct

sql - MySQL:获取数据库或表或列的字符集?