How to draw string in other device orientations using CGContext iOS

此生再无相见时 提交于 2019-12-11 02:07:40

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/6558764/how-to-draw-string-in-other-device-orientations-using-cgcontext-ios

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