How to rotate text drawn by Quartz on iPhone

梦想的初衷 提交于 2019-11-30 07:41:58

In Quartz, the image will be upside down because its Y-Axis is inverted.

iPhone's Y axis goes positive from top to bottom i.e. origin is at top left while in Quartz and OpenGL, the co-ordinates are the same way as good old geometry i.e. origin at bottom left.

Here is some code for solving Quartz inversion problem:

CGContextRef ctx = UIGraphicsGetCurrentContext();
// Tanslate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(ctx, 0.0, rect.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

Here is an interesting post about it where the author says a team in Singapore gave up an iPhone project due to this inversion problem which they could not figure out: http://trandangkhoa.blogspot.com/2009/07/iphone-os-drawing-image-and-stupid.html

Another way to handle this would be to replace the Quartz text drawing code (from CGSelectFont() to CGContextShowTextAtPoint ()) with something like

[text drawAtPoint:CGPointMake(0.0f, 30.0f) withFont:[UIFont fontWithName:@"Courier" size:40.0f]];

where text is an NSString. On the iPhone, -drawAtPoint:withFont: automatically flips the text to account for the inverted Y axis of the UIViews.

This approach gives you the advantage of handling a wider character set than the MacRoman encoding in the pure Quartz text drawing can. However, it is slightly slower.

What do you mean, exactly, by "rotate them to the normal mode"?

Assuming you just mean rotation within a 2D space, you would typically use a transform matrix. Have a look at CGAffineTransformMakeRotation.

You could apply it to the view itself (by setting the transform property), or to the CG context you have.

Well this solved my problem

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