Quote:
Originally Posted by danielb21
Spent more time today exploring the [NSString drawInRect:] method to see if I could get it to work, and I now have a working solution. The key is that you have to push the graphics context onto the stack. Below is how it works:
Code:
NSString *string = @"Text to print.";
UIGraphicsPushContext(context);
CGContextTranslateCTM(context, 0, 20);
CGContextScaleCTM(context, 1.0, -1.0);
[string drawInRect:CGRectMake(0, 0, 300, 200) withFont:[UIFont fontWithName:@"Verdana" size:9]];
UIGraphicsPopContext();
So, the above code block pushes the context onto the stack, reverses the coordinates so that the text is drawn correctly, and then uses the drawInRect: withFont: method to draw the text, and finally calls the UIGraphicsPopContext() method.
It's important to note that each time you call UIGraphicsPushContext(context); you must also call the pop context method, otherwise you'll be left with nothing.
Thanks to decay for pushing me to investigate this more and enjoy everyone!
|
Your welcome, Daniel. Actually, I only call UIGraphicsPushContext() at the very end of the document. In between, I also use [UIImage drawInRect:] to stick pictures in the document, as well as drawing a line graph. I am generating PDFs on the fly, sometimes in the 17-20 page range. My app that uses this will be submitted to Apple tomorrow actually.