Sunday, January 16, 2011

How to write text in an iPhone CGContextShowTextAtPoint

Hi,

Today I need to write a text in my iPhone using the CGContextShowTextAtPoint. First problem is that by default the text will appear inverted so we need to tranform it. The solution is for exmaple:

void drawText (CGContextRef myContext, char *title, int x, int y, int w, int h)
{
CGContextSelectFont (myContext, "Arial", h/10, kCGEncodingMacRoman);
CGContextSetRGBFillColor (myContext, 0, 0, 0, 0.99);
CGContextSetTextMatrix(myContext, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
CGContextShowTextAtPoint (myContext, x, y, title, strlen(title));
}


The key is on the
CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0)
transformation.

Hope help!!

No comments:

Post a Comment