Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xArtx

macrumors 6502a
Original poster
Mar 30, 2012
764
1
Hi Guys,
I'm following an example supplied by Apple here:

https://developer.apple.com/library...tual/drawingwithquartz2d/dq_text/dq_text.html

to draw rotated text to the screen.

I call this function:

Code:
void MyDrawText (CGContextRef myContext, CGRect contextRect) // 1
{
    float w, h;
    w = contextRect.size.width;
    h = contextRect.size.height;
    
    CGAffineTransform myTextTransform;
    CGContextSelectFont (myContext,
                         "Helvetica-Bold",
                         8,
                         kCGEncodingMacRoman);
    
    CGContextSetRGBFillColor (myContext, 0, 1, 0, .5);
    myTextTransform =  CGAffineTransformMakeRotation  (45 * 57.29578);
    CGContextSetTextMatrix (myContext, myTextTransform);
    CGContextShowTextAtPoint (myContext, screenx, screeny, linelabel, 15);
}

with this code from within my CGRect function:

Code:
                                                CGContextRef theContext = UIGraphicsGetCurrentContext();
                                                CGRect viewBounds = self.bounds;
                                                
                                                CGContextTranslateCTM(theContext, 0, viewBounds.size.height);
                                                CGContextScaleCTM(theContext, 1, -1);
                                                
                                                MyDrawText(theContext, viewBounds);


The text prints properly at 45 degrees from the point, but it also causes all
other Quartz 2d drawn text on the screen to become rotated as well.
It also messes up all other graphics including lines and polygons.

I should state that everything is fine until a frame occurs where rotated text is printed.

Probably something simple, as I'm new to Obj-C, and writing as much as possible in C.

How can I fix this?
Cheers, Art.
 
CGContextSaveGState()/CGContextRestoreGState() around the graphics state that you want to not affect other drawing.

You probably shouldn't use CGContextShowTextAtPoint(). Instead use the methods in UIStringDrawing.h. CGContextShowTextAtPoint() only draws text in MacRoman encoding.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.