CTFontGetGlyphsForCharacters总是返回false

Joe*_*ith 5 core-text ios

请帮我理解以下代码的问题:

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.");
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

rob*_*off 11

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

您需要将字符串的字符作为一个数组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.");
Run Code Online (Sandbox Code Playgroud)