0

I want to show a NSAttributedString in a PDF document.

Creating the PDF is working well but there is only plain text without any attributes.

If I change:

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

to"

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

the code is not working anymore.

Thats the code a actually wrote:

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

Thanks for help!

1 Answers1

0

It seems like it a bug in iOS7. Follow the bellow threads.

Stackoverflow Thread, Another Thread

Community
  • 1
  • 1
Ragu
  • 402
  • 1
  • 4
  • 18