CoreText Framesetter with Quartz 2d - Text is Drawing from Bottom to Top - iOS

249 views Asked by At

I am using the following code to draw a Core Text text frame which is then added to Quartz 2D drawn shape. The problem is that the text is drawn from bottom to top, instead of top to bottom (see screenshot). Can anyone help ?

- (void)drawText:(CGContextRef)contextP startX:(float)x startY:(float)
y withText:(NSString *)standString
{
    CGContextSetTextMatrix(contextP, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));

    CGRect frameText = CGRectMake(0, 0, (right-left)*2, (bottom-top)*2);

    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"This is a test string to test the string"];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attrString));
    struct CGPath * p = CGPathCreateMutable();
    CGPathAddRect(p, NULL, frameText);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL);
    CTFrameDraw(frame, contextP);
}

enter image description here

1

There are 1 answers

3
Greg On BEST ANSWER

In Quartz 2D there are different coordinate system. You have to flip the context, you can use:

CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);

It works for me.