簡體   English   中英

用Quartz 2D繪制中文字體標志符號?

[英]Draw chinese font glyphs with Quartz 2D?

我必須使用以下功能顯示漢字:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM