部分unicode字符在iOS上无法显示,但在macOS上可以正常显示。同样,iOS 可以显示的一些 unicode 字符在 watchOS 上无法显示。这是由于这些平台上安装了不同的内置字体。
当无法显示字符时,它显示为 ?在一个盒子里,像这样:
我还看到一些角色显示为外星人(不确定为什么不同):
有没有办法知道给定一个 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/