简体   繁体   中英

Why does my string get cut off when writing to a PDF

I am concatenating a number of strings into an uber string, which is then drawn to . However, for some mysterious reason, the string is prematurely discontinued at line 48 . I have confirmed that the string itself contains all the desired information at all the proper times, which indicates the issue lies elsewhere.

What could be occurring?

@interface Review ()
{
    CGSize pagesize;
    UIDocumentInteractionController *documentInteractionController;
}

...
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 3000), nil); 
...

// prepare string ... we're in a loop
// each addition is typically a single line and either a word or number
// as you can see the first string is itself. it's possible i'm doing this in a hacky way, but i am preparing pdfText in multiple stages identical as below in the loop and decided to use on string instead of many.
// i can post the entire loop if requested, but i don't see any additional useful information it provides as it's simply a series of the code line below
pdfText = [NSString stringWithFormat:@"\n%@\n%@\n%@\n%@\n%@\n%@\n", pdfText, _fd.t2_tripNumber[i], _fd.t2_departure[i], _fd.t2_outFuel[i], _fd.t2_startHobbs[i], _fd.t2_whoIsFlying[i]];


if(i == 0)
{
     // for reasons undetermined, i have to offset by a large negative value to format the text properly. if set to 0, the text is near the middle of the page??? i'm thinking this is a clue.
     // if pdfText is set to a single line, the negative bias is unneeded. wtf?
        rect = CGRectMake(offset, -103, pagesize.width, pagesize.height);
 }else{
        rect = CGRectMake(offset, 12, pagesize.width, pagesize.height);
 }  

 [pdfText drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

Here is an image which illustrates the issue with special notice to the vertical offset (the date information on the right column should align with the Date label on the left column). There should be roughly 9 more entries of data, which is cut off prematurely. It's apparent when the formatting is correctly but it's pretty clear anyway.:

http://i.imgur.com/zNVXxIH.jpg

And here is the body of code which calls the code above (inside generate)

pagesize = CGSizeMake(612, 792);
NSString *fileName = @"AirShare.pdf";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [path objectAtIndex:0];
NSString *PDFpathWithFileName = [docDirectory stringByAppendingPathComponent:fileName];

[self generate:PDFpathWithFileName];

NSURL *URL = [NSURL fileURLWithPath:PDFpathWithFileName];
if (URL) {
    self->documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
    [self->documentInteractionController setDelegate:self];
    [self->documentInteractionController presentPreviewAnimated:YES];
} 

My pagesize was too small

pagesize = CGSizeMake(612, 1200); // the 2nd argument was set to 792

Why this doesn't cut off the ENTIRE page, I don't know, but this did solve the immediate posted issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM