How to check if a character is supported by a font

限于喜欢 提交于 2019-11-30 09:05:11

问题


I'm working on an app with a text field. The text wrote in this field will be printed and I have an issue with some characters like emoji, chinese characters, etc... because the font do not provide these characters.

It's why I want to get all the character provided by a font (The font is downloaded so I can deal directly with the file or with an UIFont object).

I heard about CTFontGetGlyphsForCharacters but I'm not sure that this function do what I want and I can't get it work.

Here is my code :

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.");

Here CTFontGetGlyphsForCharacters return false. It's what I want because the character '🐯' is not provided by the font used.
The problem is when I replace NSString *characters = @"🐯" by NSString *characters = @"abc", CTFontGetGlyphsForCharacters return again false. Obviously, my font provide a glyph for all the ASCII characters.


回答1:


I finally solve it :

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


来源:https://stackoverflow.com/questions/23136073/how-to-check-if-a-character-is-supported-by-a-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!