Search code examples
iosobjective-ctextcore-graphicsquartz-graphics

Using Multiple CGAffineTransforms On Text Matrix


I am displaying text using Quartz. Here is my code:

    CGContextRef myContext = UIGraphicsGetCurrentContext();
    CGContextSelectFont(myContext, "Helvetica", 12, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing(myContext, 8);
    CGContextSetTextDrawingMode(myContext, kCGTextFillStroke);
    CGContextSetRGBFillColor(myContext, 0, 0, 0, 1);
    CGContextSetRGBStrokeColor(myContext, 0, 0, 0, 1);
    CGContextSetTextMatrix(myContext,CGAffineTransformMake(1, 0, 0, -1, 0, 0));
    CGContextShowTextAtPoint(myContext, textOrigin.x, textOrigin.y,[way.name UTF8String],[way.name length]);

This displays my text the right way up and in the right direction, however I also need to add a rotation to the text using CGAffineTransformMakeRotation(angle);. I can't seem to work out how to aply two affine transforms to the text matrix, though, without one overwriting the other. Any help would be great.


Solution

  • You can combine matrices with CGAffineTransformConcat, e.g.

    CGAffineTransform finalTransf = CGAffineTransformConcat(t1, t2);
    

    If you just need to apply rotation to an existing matrix, use CGAffineTransformRotate, e.g.

    CGAffineTransform t = CGAffineTransformMake(1, 0, 0, -1, 0, 0);
    CGAffineTransform t = CGAffineTransformRotate(t, M_PI/2);
    CGContextSetTextMatrix(myContext, t);