为什么在写入PDF时我的字符串会被切断

时间:2013-05-23 05:52:50

标签: ios objective-c

concatenating将一些字符串转换为超级字符串,然后将其绘制为 PDF 。但是,由于一些神秘的原因,该字符串在line 48过早停止。我已经确认字符串本身在所有适当的时间包含所有所需的信息,这表明问题出在其他地方。

可能会发生什么?

这是我的代码:

@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];

这是一张图片,说明了垂直偏移的特殊注意事项(右栏上的日期信息应与左栏上的日期标签对齐)。应该还有大约9个数据条目,这些条目过早被切断。很明显格式化时是正确的,但无论如何都非常清楚。:

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

以下是调用上面代码的代码体(内部生成)

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];
} 

1 个答案:

答案 0 :(得分:1)

我的网页大小太小了

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

为什么这不会切断整个页面,我不知道,但这确实解决了立即发布的问题。