27

enter image description here

I want to draw text use Quartz 2D. The "menu" direction is wrong. I want "Menu" still readable and have 45 degree with X-axis. Below is my code:

CGContextSelectFont(context, "Arial", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);

CGContextSetRGBFillColor(context, 0, 0, 0, 1); // 6 
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);

CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));   
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(45)); 
CGContextShowTextAtPoint(context,10, 10, "Menu", 4);
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167

2 Answers2

114

CGAffineTransformMakeRotation expects angle in radians, not in degrees.

#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)
...

CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 7
    You should put extra parenthesis around `x` in the macro to make it safe to use with expressions like `30+15`. With you macro that would lead to `(M_PI * 30+15 / 180.0)` which is the same as `((M_PI*30) + (15/180.0))`. A safer macro would be `#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)` – David Rönnqvist Jan 07 '14 at 14:16
5
[view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74