How to rotate text drawn by Quartz on iPhone

前端 未结 4 633
情歌与酒
情歌与酒 2020-12-30 18:37

I want to draw some texts using Quartz. Now the app draws , but I want it to show as \"0123456789\". Is there any way to show its normal orientation?

Here is my cod

相关标签:
4条回答
  • 2020-12-30 19:17

    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.

    0 讨论(0)
  • 2020-12-30 19:20

    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

    0 讨论(0)
  • 2020-12-30 19:23

    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.

    0 讨论(0)
  • 2020-12-30 19:35

    Well this solved my problem

    CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
    
    0 讨论(0)
提交回复
热议问题