title: How do I rotate text in Quartz running on iOS so the origin does not move?
body: <p>I am trying to create a PDF that will run on iOS devices (iPad only). I have created my PDF and I am able to write text and graphics into it. But I am stumped on writing rotated text such that the text will rotate but the origin will remain the same. Here is an image of the output I am trying to achieve:</p>
  
  <p><img src="https://i.stack.imgur.com/rETL4.jpg" alt="example of desired output - rotated text"></p>
  
  <p>I create my PDF like this:</p>
  
  <pre><code>  -(void)drawPDF:(NSString*)fileName
  {
      self.pageSize = CGSizeMake(792, 612);
  
      // Create the PDF context using the default page size of 612 x 792.
      UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
  
      // Mark the beginning of a new page.
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
  
      // Get the graphics context.
      context = UIGraphicsGetCurrentContext();
  
      CGContextSetTextMatrix(context, CGAffineTransformIdentity);    
      CGContextTranslateCTM(context, 0, 654);
      CGContextScaleCTM(context, 1.0, -1.0);
  
      UIFont *font = [UIFont fontWithName:@"Helvetica-Light" size:8];
  
      [self drawText:@"here is a really long line of text so we can see" withFrame:CGRectMake(400, 100, 50, 50) withFont:font rotation:-50];
  
      // Close the PDF context and write the contents out.
      UIGraphicsEndPDFContext();
  }
  </code></pre>
  
  <p>Then I have this function for drawing the text, that I've tried many variations of:</p>
  
  <pre><code>  -(void)drawText:(NSString *)text withFrame:(CGRect)rect withFont:(UIFont *)font rotation:(float)degrees
  {
      float radians = [PDFMaker DegreesToRadians:degrees];
  
      NSDictionary *attribs = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Light" size:10.0]};
  
      CGContextSaveGState(context);
  
      CGPoint pos = CGPointMake(rect.origin.x, -50 - rect.origin.y);
  
      // Translate so we're drawing in the right coordinate space
      CGContextScaleCTM(context, 1.0, -1.0);
  
      CGSize stringSize = [text sizeWithAttributes:attribs];
      CGContextTranslateCTM(context, 0-stringSize.width/2, 0-stringSize.height/2);
  
      CGContextRotateCTM(context, radians);
  
      CGContextTranslateCTM(context, stringSize.width/2, stringSize.height/2);
  
      [text drawAtPoint:CGPointMake(pos.x, pos.y) withAttributes:attribs];
  
      CGContextRestoreGState(context);
  }
  </code></pre>
  
  <p>But no matter how much I play with the translates, the text jumps around as if being rotated around an axis at the origin. I've read in other answers to rotate the text on its center at the origin and have tried that but that doesn't seem to work (or, probably more accurately, I can't get it to work). I suspect the problem may be something related to using a PDF context but really don't know. Any help would be greatly appreciated.</p>