0

我必须使用以下功能显示汉字:

CG_EXTERN void CGContextShowGlyphsAtPoint(CGContextRef context, CGFloat x,
CGFloat y, const CGGlyph glyphs[], size_t count)

但它没有准确显示。我使用的代码如下:

CGFontRef cgfont = CGFontCreateWithFontName((CFStringRef)label.font.fontName);
CGContextSetFont(theContext, cgfont);
CGContextSetFontSize(theContext, label.font.pointSize);
CGContextSetTextDrawingMode (theContext, kCGTextClip);

CGGlyph *glyphs = malloc(sizeof(CGGlyph) * [label.text length]);
char *Chars = malloc(sizeof(char) * ([label.text length] + 1));
[label.text getCString:Chars maxLength:([label.text length] + 1) encoding:NSISOLatin2StringEncoding];

for(int currentChar = 0; currentChar < [label.text length]; ++currentChar)
{
    glyphs[currentChar] = Chars[currentChar];
}
CGContextShowGlyphsAtPoint(theContext, 0, (size_t)label.font.ascender, glyphs, [label.text length]);

编辑

设备是 iPhone。例如,我想显示像“中文”这样的汉字,但是使用CGContextShowGlyphsAtPoint来绘制字符串会显示像这样“@#Radcx67”。

如何解决这个问题呢?谢谢!

4

1 回答 1

1

首先,包括这个

 #import "CoreText/CTFont.h"

然后请使用以下功能。

 void drawStringWithglyphs(CTFontRef iFont, CFStringRef iString, CGContextRef ctx, int x, int y)

{
UniChar *characters;
CGGlyph *glyphs;
//    CGPoint *points;
CFIndex count;

assert(iFont != NULL && iString != NULL);

// Get our string length.
count = CFStringGetLength(iString);

// Allocate our buffers for characters and glyphs.
characters = (UniChar *)malloc(sizeof(UniChar) * count);
assert(characters != NULL);

glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * count);
assert(glyphs != NULL);

// Get the characters from the string.
CFStringGetCharacters(iString, CFRangeMake(0, count), characters);

CTFontGetGlyphsForCharacters(iFont, characters, glyphs, count);


// Do something with the glyphs here, if a character is unmapped
    CGContextShowGlyphsAtPoint(ctx, x, y, glyphs, count);

// Free our buffers
free(characters);
free(glyphs);
}
于 2012-07-01T04:13:47.300 回答