如何使用CGContext iOS在其他设备方向上绘制字符串

时间:2011-07-02 18:15:06

标签: iphone ios ipad cgcontext

我正在尝试在iPhone上绘制一些文字,但在横向模式下,主页按钮位于左侧。所以我需要以某种方式扭曲数字(我猜我使用了变换)?

这就是我现在所拥有的:

    CGContextSetRGBFillColor(context, 1.0, 0, 0, 1.0);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(M_PI/2));
    CGContextSetTextDrawingMode(context, kCGTextFill);
    NSString *t = [NSString stringWithFormat:@"%i",seconds];
    const char *str=[t UTF8String];
    CGContextShowTextAtPoint(context,6.0,15.0,str,strlen(str));

我需要添加什么才能旋转文字,以便在手机处于横向时可以通过左侧的主页按钮进行阅读。

1 个答案:

答案 0 :(得分:0)

您必须应用两个转换矩阵。第一个将翻转文本,第二个将旋转它。请尝试以下代码:

CGContextSetRGBFillColor(context, 1.0, 0, 0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
CGAffineTransform xform = CGAffineTransformMake(
                                                1.0,  0.0,
                                                0.0, -1.0,
                                                0.0,  0.0);
CGContextSetTextMatrix(context, xform);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(M_PI_2));
CGContextSetTextDrawingMode(context, kCGTextFill);
NSString *t = [NSString stringWithFormat:@"%i",seconds];
const char *str=[t UTF8String];
CGContextShowTextAtPoint(context,6.0,15.0,str,strlen(str));