smartsanja
smartsanja

Reputation: 4540

Draw NSString on to a PDF page

In my application, I'm creating a PDF. I'm able to draw single line string. But I want to draw a string with multiple lines. Here's my code

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = @"test.pdf";
NSURL *fileURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]];

// Create PDF context
CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, NULL, NULL);
CGPDFContextBeginPage(pdfContext, NULL);
UIGraphicsPushContext(pdfContext);

// Flip coordinate system
CGRect bounds = CGContextGetClipBoundingBox(pdfContext);
CGContextScaleCTM(pdfContext, 1.0, -1.0);
CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height);

// String to show
NSString *strTest=[[NSString alloc]initWithFormat:@"%@\n%@", @"one",@"two"];
NSLog(@"%@", strTest);
// Drawing commands

[strTest drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:12.0f]];

// Clean up
UIGraphicsPopContext();
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);

This is creating a PDF with sigle lined string.

One Two

But I want to draw the string as it is (two line string)

One
Two

How can I modify this code to draw a string with multiple lines?

Upvotes: 0

Views: 331

Answers (1)

rob mayoff
rob mayoff

Reputation: 385510

You need to use the drawInRect:withFont: method, or one of the other drawInRect:... methods.

Upvotes: 1

Related Questions