iOS: Mixing Core Graphics drawing layout with UIKit draw text functions

advertisements

I am using following context in UIView's extended class's drawrect method to draw Paths and Strings.

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);

To Draw Path I use

CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0f);
CGContextSetLineWidth(context, 1);
CGContextBeginPath(context);
CGContextMoveToPoint(context, origin.x, origin.y);
CGContextAddLineToPoint(context, currentX, origin.y);
......
CGContextStrokePath(context);

To Draw text I use

CGContextSetLineWidth(context, 2.0);
[self.title drawInRect:CGRectMake(100, 100, 200, 40) withFont:font];

I get paths correctly but Text gets up side down! If I remove CGContextScaleCTM and CGContextTranslateCTM I get path up side down! Can someone help me to resolve this please.


Save your previous context before drawing your path and restore it afterward:

CGContextRef context = UIGraphicsGetCurrentContext();

// save the original context
CGContextSaveGState(context);

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);

// draw path

// restore the context
CGContextRestoreGState();

// draw text

That should do it.