【问题标题】:Show attributedString as PDF将属性字符串显示为 PDF
【发布时间】:2014-06-12 16:31:32
【问题描述】:

我想在 PDF 文档中显示一个 NSAttributedString。

创建 PDF 效果很好,但只有纯文本,没有任何属性。

如果我改变:

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

到"

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

代码不再工作了。

这是a实际写的代码:

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

感谢您的帮助!

【问题讨论】:

    标签: ios objective-c pdf nsattributedstring


    【解决方案1】:

    这似乎是 iOS7 中的一个错误。按照下面的线程。

    Stackoverflow Thread, Another Thread

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 2015-12-11
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多