使用CGContextShowAtPoint时,为什么我的文字会翻转?

use*_*007 6 iphone objective-c

我正在写一个简单的练习.但是,当我尝试使用CGContext在UIView上放置一些字符串时,我的文字翻转过来,我想知道为什么以及如何将其更改为正确的格式.

这是我在drawRect中的代码

    char *string = "TEST";
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSelectFont (context,"Helvetica-Bold",12, kCGEncodingMacRoman); 

CGContextShowTextAtPoint(context, 5, 5, string, strlen(string));

CGContextClosePath(context);
Run Code Online (Sandbox Code Playgroud)

Thanx的帮助.

Art*_*pie 5

CoreGraphics使用笛卡尔坐标,因此您需要在进行任何绘图之前翻译上下文

CGContextRef context = UIGraphicsGetCurrentContext();

// transforming context
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// your drawing code
Run Code Online (Sandbox Code Playgroud)


Rog*_*ger 5

Quartz2D有一个倒y轴 - 方便吗?如果您在drawRect方法中,则可以使用以下内容翻转文本.

CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
Run Code Online (Sandbox Code Playgroud)

另一种方式是;

transform = CGAffineTransformMake(1.0,0.0,0.0,-1.0,0.0,0.0);
CGContextSetTextMatrix(context, transform);
Run Code Online (Sandbox Code Playgroud)

或者在一条线上;

CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
Run Code Online (Sandbox Code Playgroud)