Rotated text with Quartz 2D

Discussion in 'iOS Programming' started by xArtx, May 5, 2012.

  1. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #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.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     

Share This Page