Ser Pounce
Ser Pounce

Reputation: 14547

Translating Graphics Context

In the following code below, I'm trying to translate, or more a graphics context for which I'm using CoreText to draw on. The first four lines of code flip the coordinate system as CoreText uses the origin from the lower left hand corner. After the coordinate system is flipped I need to move the graphics context down the y axis by textFrameOriginY amount. However the following code I'm using isn't working. I'm not that familiar with graphics and was wondering if there is something I'm doing wrong?

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextTranslateCTM(context, 0, textFrameOriginY);

Upvotes: 1

Views: 929

Answers (2)

Vignesh
Vignesh

Reputation: 10251

This line should be,

CGContextTranslateCTM(context, 0, viewHeight-textFrameOriginY);

to get proper placing.

Upvotes: 1

ikuramedia
ikuramedia

Reputation: 6058

After you've flipped the coordinate syste, down is negative and up is positive, so your second translate should be -textFrameOriginY?

Assuming that this is in -drawRect: - remember that you can't draw outside of your bounds, that should help you with debugging the issue...

Upvotes: 2

Related Questions