请使用以下代码帮助我理解问题:
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.");
感谢任何帮助。
答案 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.");