Using Multiple CGAffineTransforms On Text Matrix

我的未来我决定 提交于 2019-12-10 17:11:26

问题


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.


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/6047972/using-multiple-cgaffinetransforms-on-text-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!