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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top