从UITextView创建PDF

Hen*_*y F 3 pdf cocoa-touch quartz-2d ios

我目前正在创建和保存UIView的PDF.它工作得很好,但现在我正在尝试从保存到磁盘而不是UIView的文本数据创建PDF.理想情况下,我想基于UITextView中的文本创建PDF.下面的代码很好地编写了文本视图的第一部分,但它没有从整个文本视图创建PDF.这是相当多的文本,因此它只根据适合视图的文本量创建PDF.我目前使用的代码是:

- (void)createPDFfromUIView:(UITextView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{

   NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks));

    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, textview.bounds, myDictionary);
    UIGraphicsBeginPDFPage();

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [textview.layer renderInContext:pdfContext]; // this line

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"filename"];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

并将其称为/保存,我将此消息发送到IBAction中: [self createPDFfromUIView:textview saveToDocumentsWithFileName:@"pdflocation"];

现在,我已经解析了几个小时的代码和文档,并且无法弄清楚如何基于保存的文本文件而不是视图来创建PDF.我还是编程的新手,所以很多这都是我的想法.有谁知道如何更改上面的代码,让它根据保存的文本而不是视图创建PDF?

选项:我可以根据NSString的输出创建PDF.我也可以将保存的文本放入UITextView并根据(原始计划)创建PDF但我不知道该怎么做.

Mih*_*ncu 5

Apple的这篇文章解释了如何在Pdf页面上创建Pdf文件和绘制文本.