简体   繁体   English

CTFontGetGlyphsForCharacters总是返回false

[英]CTFontGetGlyphsForCharacters always returns false

Please help me understand the problem with the following code: 请帮我理解以下代码的问题:

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

Any help is appreciated. 任何帮助表示赞赏。

You are getting a C string containing UTF-8 encoded characters and then casting it to unichar * . 您将获得包含UTF-8编码字符的C字符串,然后将其转换为unichar * That won't work. 那不行。 A unichar is a 16-bit UTF-16-encoded character. unichar是一个16位UTF-16编码字符。 A simple C cast won't convert the character encoding. 简单的C cast不会转换字符编码。

You need to ask the string for its characters as an array of unichar : 你需要将字符串的字符作为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.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM