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开发者_如何学C one overwriting the other. Any help would be great.
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);
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.
精彩评论