我正在开发一个带有文本字段的应用程序。在此字段中写入的文本将被打印出来,我对某些字符(如表情符号、汉字等)有疑问...因为字体不提供这些字符。
这就是为什么我想获取字体提供的所有字符(下载字体以便我可以直接处理文件或 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/