【问题标题】:How to check if a character is supported by a font如何检查字体是否支持字符
【发布时间】:2014-06-01 21:33:00
【问题描述】:

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

这就是为什么我想获取字体提供的所有字符(字体已下载,因此我可以直接处理文件或 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 = @"abc" 替换NSString *characters = @"????" 时,CTFontGetGlyphsForCharacters 再次返回false。显然,我的字体为所有 ASCII 字符提供了一个字形。

【问题讨论】:

标签: ios objective-c fonts uifont emoji


【解决方案1】:

我终于解决了:

- (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;
}

【讨论】:

  • 很确定您仍然需要在返回之前 CFRelease 该字体,即使在 ARC 中也是如此。 CF 类型不受 ARC 影响。
  • 这一切肯定意味着 CTFontGetGlyphsForCharacters 中存在错误?您不必每次都创建 CTFont...
猜你喜欢
  • 2011-07-13
  • 2018-06-05
  • 1970-01-01
  • 2013-11-06
  • 2016-11-04
  • 2020-02-02
  • 2019-08-17
  • 1970-01-01
相关资源
最近更新 更多