如何使用CGContext iOS在其他设备方向上绘制字符串 [英] How to draw string in other device orientations using CGContext iOS

查看:207
本文介绍了如何使用CGContext iOS在其他设备方向上绘制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to draw some text on the iPhone, but in landscape mode, where the home button is on the left. So I need to somehow twist the numbers (I'm guessing I use a transform) ?

这就是我现在所拥有的:

Here's what I have now:

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

我需要添加什么来旋转文字,以便在手机风景时可以阅读使用左侧的主页按钮。

What do I need to add to rotate the text so that it can be read when the phone is landscape with the home button on the left.

推荐答案

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

You have to apply two transformation matrices. The first one will flip the text and the second one will rotate it. Try the following code:

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

这篇关于如何使用CGContext iOS在其他设备方向上绘制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆