将attributedString显示为PDF

时间:2014-06-12 16:31:32

标签: ios objective-c pdf nsattributedstring

我想在PDF文档中显示NSAttributedString。

创建PDF工作正常,但只有纯文本没有任何属性。

如果我改变:

CFAttributedStringRef currentText = CFAttributedStringCreateCopy(NULL,(__bridge CFAttributedStringRef)**enterText.text**);

为“

CFAttributedStringRef currentText = CFAttributedStringCreateCopy(NULL,(__bridge CFAttributedStringRef)**enterText.attributedText**);

代码不再有效了。

这是实际写的代码:

- (IBAction)createPDF:(id)sender {

//Get Document Directory path
NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//Define path for PDF file
documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"/Editortext.pdf"];

// Prepare the text using a Core Text Framesetter.
 CFAttributedStringRef currentText = CFAttributedStringCreateCopy(NULL,(__bridge CFAttributedStringRef)enterText.text);

if (currentText) {
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
    if (framesetter) {


        // Create the PDF context using the default page size of 612 x 792.
        UIGraphicsBeginPDFContextToFile(documentPath, CGRectZero, nil);

        CFRange currentRange = CFRangeMake(0, 0);
        NSInteger currentPage = 0;
        BOOL done = NO;

        do {
            // Mark the beginning of a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

            // Draw a page number at the bottom of each page.
            currentPage++;
            [self drawPageNbr:currentPage];

            // Render the current page and update the current range to
            // point to the beginning of the next page.
            currentRange = *[self updatePDFPage:currentPage setTextRange:&currentRange setFramesetter:&framesetter];

            // If we're at the end of the text, exit the loop.
            if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))
                done = YES;
        } while (!done);

        // Close the PDF context and write the contents out.
        UIGraphicsEndPDFContext();

        // Release the framewetter.
        CFRelease(framesetter);

       // [self pdfsenden];

    } else {
        NSLog(@"Could not create the framesetter..");
    }
    // Release the attributed string.
    CFRelease(currentText);
} else {
    NSLog(@"currentText could not be created");
}
 }

 -(CFRange*)updatePDFPage:(int)pageNumber setTextRange:(CFRange*)pageRange setFramesetter:(CTFramesetterRef*)framesetter
{
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect frameRect = CGRectMake(72, 72, 468, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(*framesetter, *pageRange,
                                               framePath, NULL);
CGPathRelease(framePath);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 792);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Update the current range based on what was drawn.
*pageRange = CTFrameGetVisibleStringRange(frameRef);
pageRange->location += pageRange->length;
pageRange->length = 0;
CFRelease(frameRef);
return pageRange;
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这似乎是iOS7中的一个错误。按照波纹管螺纹。

Stackoverflow ThreadAnother Thread