4

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.

STW
  • 44,917
  • 17
  • 105
  • 161
StuStirling
  • 15,601
  • 23
  • 93
  • 150

2 Answers2

12

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);
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

To supplement @kennytm's answer, to apply more than two transforms you can do the following:

var t = CGAffineTransformIdentity
t = CGAffineTransformTranslate(t, CGFloat(100), CGFloat(300))
t = CGAffineTransformRotate(t, CGFloat(M_PI_4))
t = CGAffineTransformScale(t, CGFloat(-1), CGFloat(2))
// ... add as many as you want, then apply it to to the view
imageView.transform = t

Go here to see my full answer.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • You are on the way, but applying order of transforms is very important. According to that, the result will be different apparently. – softninja Oct 27 '16 at 18:03