ios - 如何检查字体是否支持字符

标签 ios objective-c fonts uifont emoji

我正在开发一个带有文本字段的应用程序。在此字段中写入的文本将被打印出来,我对某些字符(如表情符号、汉字等)有疑问...因为字体不提供这些字符。

这就是为什么我想获取字体提供的所有字符(下载字体以便我可以直接处理文件或 UIFont 对象)。

我听说过 CTFontGetGlyphsForCharacters,但我不确定这个函数是否能满足我的需求,但我无法让它发挥作用。

这是我的代码:

CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
NSString *characters = @"🐯"; // emoji character
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
    NSLog(@"CTFontGetGlyphsForCharacters failed.");

此处 CTFontGetGlyphsForCharacters 返回 false。这是我想要的,因为使用的字体没有提供字符'🐯'。
问题是当我将 NSString *characters = @"🐯" 替换为 NSString *characters = @"abc" 时,CTFontGetGlyphsForCharacters 再次返回 错误。显然,我的字体为所有 ASCII 字符提供了一个字形。

最佳答案

我终于解决了:

- (BOOL)isCharacter:(unichar)character supportedByFont:(UIFont *)aFont
{
    UniChar characters[] = { character };
    CGGlyph glyphs[1] = { };
    CTFontRef ctFont = CTFontCreateWithName((CFStringRef)aFont.fontName, aFont.pointSize, NULL);
    BOOL ret = CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, 1);
    CFRelease(ctFont);
    return ret;
}

关于ios - 如何检查字体是否支持字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23136073/

相关文章:

iphone - 如何以编程方式创建 KMZ 文件?

ios - 错误 : Attempt to insert item 0 into section 0, 但更新后第 0 部分只有 0 个项目

objective-c - UITableViewCell 的 backgroundView 重叠 contentView

ios - caretRectForPosition 的默认值是什么或如何设置确定类覆盖的 BOOL

ios - uifont 'Impact' 不适用于 iOS

windows - 获取 DrawText [Ex] 解释的 GDI HFONT 行高

html - 如何在特定字体下调用所有谷歌字体 CSS

objective-c - 等到 UI 更新后再删除 UIActivityIndi​​cator

objective-c - 最简单的 Cocoa Binding 示例

css - 别名 Font Awesome 类