2014-06-12 2 views
0

PDF 문서에 NSAttributedString을 표시하려고합니다.attributedString을 PDF로 표시

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

감사합니다 도움을 청합니다!

답변