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

3bygqnnd  于 2023-11-19  发布在  iOS
关注(0)|答案(1)|浏览(157)

我正在使用一个带有文本字段的应用程序。在此字段中写入的文本将被打印,我对一些字符(如表情符号,中文字符等)有问题...因为字体不提供这些字符。
这就是为什么我想获得字体提供的所有字符(字体被下载,这样我就可以直接处理文件或UIFont对象)。
我听说过CTFontGetGlyphsForCharacters,但我不确定这个函数是否能满足我的要求,我无法让它工作。
下面是我的代码:

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

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

cuxqih21

cuxqih211#

我终于解决了:

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

字符串

相关问题