简体   繁体   中英

How to draw string in other device orientations using CGContext iOS

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM