我必须显示汉字,函数:
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"。
如何解决这个问题?谢谢!
首先,包括这个
#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);
}