carlos
carlos

Reputation: 2624

How to change Text Rotation Axis

I am using CGContextShowText to display text, but when rotating it using CGAffineTransformation, the rotation axis is located at the left, not at the center of the drawn text, the code I am using is this:

CGContextSaveGState(context);

CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, 1);
CGContextSetRGBFillColor (context, 0, 0, 0, 1);
CGAffineTransform xform = CGAffineTransformMake(
    sin(DegreesToRadians(angle)), cos(DegreesToRadians(angle)),
    cos(DegreesToRadians(angle)), -sin(DegreesToRadians(angle)),
    0,  0);

CGContextSetTextDrawingMode (context, kCGTextFill); 

CGPoint degreeDisplayPoint = CGPointMake(100,100);
CGContextShowTextAtPoint(context, degreeDisplayPoint.x, degreeDisplayPoint.y, [angleStringWithDegree cStringUsingEncoding:NSMacOSRomanStringEncoding], [angleStringWithDegree length]); 

CGContextRestoreGState(context);

Any ideas?

Upvotes: 0

Views: 847

Answers (1)

Zebra North
Zebra North

Reputation: 11482

Translate your object so that its centre is at (0, 0), then rotate it, then translate it back to where you want.

Upvotes: 5

Related Questions