带有Quartz 2d的CoreText Framesetter - 文本从下到上绘制 - iOS

时间:2013-12-06 11:47:29

标签: ios objective-c text quartz-2d core-text

我使用以下代码绘制Core Text文本框架,然后将其添加到Quartz 2D绘制的形状中。问题是文本是从下到上绘制的,而不是从上到下绘制(见截图)。有人可以帮忙吗?

- (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 个答案:

答案 0 :(得分:0)

在Quartz 2D中有不同的坐标系。你必须翻转上下文,你可以使用:

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

它对我有用。