Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my text flip over when using CGContextShowAtPoint?

I am writing a simple practice. However, my text is flip upside down when I trying to use CGContext to put some string on the UIView, I am wondering why and how to change it into correct format.

Here is my code in the drawRect

    char *string = "TEST";
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSelectFont (context,"Helvetica-Bold",12, kCGEncodingMacRoman); 

CGContextShowTextAtPoint(context, 5, 5, string, strlen(string));

CGContextClosePath(context);

Thanx for your help.

like image 265
user384007 Avatar asked Jul 05 '10 22:07

user384007


1 Answers

CoreGraphics uses cartesian coordinates, so you need to translate your context before doing any drawing

CGContextRef context = UIGraphicsGetCurrentContext();

// transforming context
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// your drawing code
like image 87
Art Gillespie Avatar answered Oct 13 '22 00:10

Art Gillespie