Skip to content

Drawing Unicode text with Core Graphics on the iPhone/iPad

April 21, 2011
by

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.