CTFontGetGlyphsForCharacters总是返回false

时间:2013-06-30 17:38:40

标签: ios core-text

请使用以下代码帮助我理解问题:

NSString *fontName = @"ArialMT";
CGFloat fontSize = 20.0;
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
NSString *characters = @"ABC";
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
    NSLog(@"*** CTFontGetGlyphsForCharacters failed.");

感谢任何帮助。

1 个答案:

答案 0 :(得分:11)

您将获得一个包含UTF-8编码字符的C字符串,然后将其强制转换为unichar *。那不行。 unichar是16位UTF-16编码字符。简单的C版演员不会转换字符编码。

您需要将字符串的字符作为unichar

的数组
NSString *fontName = @"ArialMT";
CGFloat fontSize = 20.0;
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
NSString *string = @"ABC";
NSUInteger count = string.length;
unichar characters[count];
[string getCharacters:characters range:NSMakeRange(0, count)];
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, characters, glyphs, count) == false)
    NSLog(@"*** CTFontGetGlyphsForCharacters failed.");