Flip text with flipping in CGContext

Image: http://i.stack.imgur.com/pbzar.png

I want to get text 90-270 degrees (text "Scent 7" to "Scent 17") rotated 180 degrees.

My code:

 for (int i=0; i<24; i++) {
    CGContextSaveGState(context);
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSString *str = [NSString stringWithFormat:@"Aroma %d", i];

    CGContextTranslateCTM(context, radius, radius);
    CGContextRotateCTM(context, i * 15 * M_PI/180.0);
    [[UIColor whiteColor] set];

    CGContextTranslateCTM(context, - (radius), -(radius));

    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12.0]
                     constrainedToSize:rect.size
                         lineBreakMode:(NSLineBreakByWordWrapping)];

    [str drawAtPoint:CGPointMake(((radius * 2) - 10) - size.width, radius) withFont:[UIFont fontWithName:@"Helvetica" size:12.0]];


    CGContextRestoreGState(context);
}

      

Thanks for the help!

+3


source to share


1 answer


This should produce the desired output:

for (int i=0; i<24; i++) {
    CGContextSaveGState(context);
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSString *str = [NSString stringWithFormat:@"Aroma %d", i];

    CGContextTranslateCTM(context, radius, radius);
    CGContextRotateCTM(context, i * 15 * M_PI/180.0);
    [[UIColor whiteColor] set];
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12.0]
                  constrainedToSize:rect.size
                      lineBreakMode:(NSLineBreakByWordWrapping)];

    CGContextTranslateCTM(context, radius-10-size.width/2, size.height/2); // (1)
    if (i >= 7 && i <= 17)
        CGContextRotateCTM(context, M_PI); // (2)
    [str drawAtPoint:CGPointMake(-size.width/2, -size.height/2) withFont:[UIFont fontWithName:@"Helvetica" size:12.0]];

    CGContextRestoreGState(context);
}

      

The idea is to move the origin to the center of the rectangle where the text should be drawn (see (1)

). Then you can simply rotate the text 180 degrees (see (2)

).



This is the output of my test program:

enter image description here

+4


source







All Articles