Drawing Unicode text with Core Graphics on the iPhone/iPad
April 21, 2011
Drawing text using Core Graphics seems so simple, just use the good old CGContextShowText or CGContextShowTextAtPoint function right?
Unfortunately these functions do have support for drawing Unicode text. You have to convert your text to ASCII just to draw it.
Fortunately, its not really that hard to draw Unicode text. I’ve extracted some code from our Showyou app that does just that:
NSMutableAttributedString *aStr = [NSMutableAttributedString attributedStringWithString:@"some UTF8 text"];
CGContextSetRGBFillColor(context,1.0,1.0,1.0,1.0);
[aStr setTextColor:[UIColor whiteColor]];
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)aStr);
CGContextSetTextPosition(context, x, y);
CGContextSaveGState(context);
CGContextClipToRect(context, clipRect); // if you want to clip the drawing
CTLineDraw(line, context);
CFRelease(line);
CGContextRestoreGState(context);
Simple!
Advertisement
No comments yet