2013-01-03 28 views
3

我有以下方法工作在我的@implementation的UIView(SaveToPdf)類精細需要渲染UITextView的全部內容以PDF

- (NSData *) toPdfData 
{ 
// 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, self.bounds, nil); 
UIGraphicsBeginPDFPage(); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 
[self.layer renderInContext:pdfContext]; 

// remove PDF rendering context 
UIGraphicsEndPDFContext(); 

return pdfData; 
} 

我用這個方法來打印我的觀點,以PDF和它工作得很好,除了一個問題。我的視圖包含一個UITextView,而pdf僅打印該UITextView的可見區域。我需要PDF來呈現完整的內容(即可滾動文字)

我是iOS開發新手,如果有人能請我指出正確的方向,我將不勝感激。有一點需要注意,我還需要在UIView中打印其他子視圖(標籤,文本框等),並將其打印到該PDF中。這目前工作正常,佈局保存在我的PDF只需使用[self.layer renderInContext:pdfContext];由於主視圖將循環遍歷所有的子視圖..

謝謝

+0

你需要創建PDF drowline和drowRect然後你可以保存爲pdf你可以得到這個 –

+0

爲什麼downvotes?試圖對新來者好。 Upvoting以彌補downvotes ... –

回答

1

我知道的方法2 -

  1. 我已經看到了一些代碼,其中[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];正在做。當我嘗試時,我看到它導致我鬆散的文本清晰。所以我不能使用這種方法。
  2. 這是比較傳統的方法。即您提取文本並繪製​​它。這段代碼非常堅固,並且已經看到了一些用法。所以你需要做的就是從你的uitextview獲得所有文本並將它傳遞到這裏...

希望這有助於。

#define kBorderInset 25.0 
#define kMarginInset 15.0 

- (void) drawText 
{ 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0); 

NSString *textToDraw = @"YOUR TEXT FROM UITEXTVIEW HERE"; 
UIFont *font = [UIFont systemFontOfSize:16.0]; 

CGSize stringSize = [textToDraw sizeWithFont:font 
          constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
           lineBreakMode:UILineBreakModeWordWrap]; 

CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 350.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); 

[textToDraw drawInRect:renderingRect 
       withFont:font 
     lineBreakMode:UILineBreakModeWordWrap 
      alignment:UITextAlignmentLeft]; 
return; 
} 
+0

感謝您的快速答覆斯里卡爾。我需要注意的一件事是我的UIView除了UITextView之外還有其他的東西......我有標籤,文本框等... [self.layer renderInContext:pdfContext];遞歸地打印所有的子視圖,使事情變得簡單。話雖如此,只有當UITextView即將渲染時,我將如何才能觸發此代碼......並保留子視圖的其餘部分不變 – hhaddad

0

我已經寫了這段代碼從UIView內容生成pdf。 願這能幫助你。

#define kBorderInset   20.0 
#define kBorderWidth   1.0 
#define kMarginInset   10.0 

//Line drawing 
#define kLineWidth    1.0 

    - (IBAction)generatePdfButtonPressed 
{ 
pageSize = CGSizeMake(612, 792); 
NSString *fileName = @"profileInfo.pdf"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; 

[self generatePdfWithFilePath:pdfFileName]; 
} 

- (void) generatePdfWithFilePath: (NSString *)thefilePath 
{ 
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 

NSInteger currentPage = 0; 
BOOL done = NO; 
do 
{ 
    // Mark the beginning of a new page. 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

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

    //Draw a border for each page. 
    [self drawBorder]; 

    //Draw text fo our header. 
    [self drawHeader]; 

    //Draw a line below the header. 
    [self drawLine]; 

    //Draw some text for the page. 
    if (profileInfo!=nil) 
    { 
    NSString *stringwithInfo=[NSString stringWithFormat:@"Headline: %@\nFirstname: %@\nLastName : %@\nSiteStandardProfileRequest:\n%@",[profileInfo valueForKey:@"headline"],[profileInfo valueForKey:@"firstName"],[profileInfo valueForKey:@"lastName"],[[profileInfo valueForKey:@"siteStandardProfileRequest"]valueForKey:@"url"]]; 


    [self drawText:stringwithInfo]; 
    } 

    //Draw an image 
    [self drawImage]; 
    done = YES; 
} 
while (!done); 

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

- (void) drawBorder 
{ 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
UIColor *borderColor = [UIColor brownColor]; 
CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2); 
CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor); 
CGContextSetLineWidth(currentContext, kBorderWidth); 
CGContextStrokeRect(currentContext, rectFrame); 
} 


- (void) drawLine 
{ 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

CGContextSetLineWidth(currentContext, kLineWidth); 

CGContextSetStrokeColorWithColor(currentContext, [UIColor blueColor].CGColor); 

CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 40.0); 
CGPoint endPoint = CGPointMake(pageSize.width - 2*kMarginInset -2*kBorderInset, kMarginInset + kBorderInset + 40.0); 

CGContextBeginPath(currentContext); 
CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y); 
CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y); 

CGContextClosePath(currentContext); 
CGContextDrawPath(currentContext, kCGPathFillStroke); 
} 


- (void) drawText:(NSString*)txtTodraw 
{ 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0); 

UIFont *font = [UIFont systemFontOfSize:14.0]; 

CGSize stringSize = [txtTodraw sizeWithFont:font 
          constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
           lineBreakMode:UILineBreakModeWordWrap]; 

CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); 

[txtTodraw drawInRect:renderingRect 
       withFont:font 
      lineBreakMode:UILineBreakModeWordWrap 
       alignment:UITextAlignmentLeft]; 
} 


- (void) drawImage 
{ 
UIImage * demoImage = [UIImage imageNamed:@"demo.png"]; 
[demoImage drawInRect:CGRectMake((pageSize.width - demoImage.size.width/2)/2, 350, demoImage.size.width/2, demoImage.size.height/2)]; 
} 


- (void) drawHeader 
{ 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(currentContext, 0.3, 0.7, 0.2, 1.0); 

NSString *textToDraw = @"LinkedIn Profile Info"; 

UIFont *font = [UIFont systemFontOfSize:24.0]; 

CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:UILineBreakModeWordWrap]; 

CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); 

[textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 
} 


- (void)drawPageNumber:(NSInteger)pageNumber 
{ 
NSString* pageNumberString = [NSString stringWithFormat:@"Page %d", pageNumber]; 
UIFont* theFont = [UIFont systemFontOfSize:12]; 

CGSize pageNumberStringSize = [pageNumberString sizeWithFont:theFont 
              constrainedToSize:pageSize 
               lineBreakMode:UILineBreakModeWordWrap]; 

CGRect stringRenderingRect = CGRectMake(kBorderInset, 
             pageSize.height - 40.0, 
             pageSize.width - 2*kBorderInset, 
             pageNumberStringSize.height); 

[pageNumberString drawInRect:stringRenderingRect withFont:theFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; 
} 

-(void)fillInformationInform:(NSDictionary*)dics 
{ 
headLineLabel.text=[dics valueForKey:@"headline"]; 
NSURL *picUrl=[NSURL URLWithString:[dics valueForKey:@"pictureUrl"]]; 
profileImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:picUrl]]; 

phoneNoLabel.text=[[[[dics valueForKey:@"phoneNumbers"] valueForKey:@"values"] objectAtIndex:0] valueForKey:@"phoneNumber"]; 

cityLabel.text=[[dics valueForKey:@"location"] valueForKey:@"name"]; 
emailAddressLabel.text=[dics valueForKey:@"emailAddress"]; 
nameLabel.text=[NSString stringWithFormat:@"%@ %@",[dics valueForKey:@"firstName"],[dics valueForKey:@"lastName"]]; 

dateOfBirthLabel.text=[NSString stringWithFormat:@"%@-%@-%@",[[dics valueForKey:@"dateOfBirth"]valueForKey:@"day"],[[dics valueForKey:@"dateOfBirth"]valueForKey:@"month"],[[dics valueForKey:@"dateOfBirth"]valueForKey:@"year"]]; 

NSDictionary *eduDic=[[NSDictionary alloc] initWithDictionary:[[[dics valueForKey:@"educations"]valueForKey:@"values"]objectAtIndex:0]]; 

// NSLog(@"education dic is %@",eduDic); 

degreeLabel.text=[eduDic valueForKey:@"degree"]; 
durationLabel.text=[NSString stringWithFormat:@"From %@ To %@",[[eduDic valueForKey:@"startDate"]valueForKey:@"year"],[[eduDic valueForKey:@"endDate"]valueForKey:@"year"]]; 
fieldOfStudyLabel.text=[eduDic valueForKey:@"fieldOfStudy"]; 
collegeLabel.text=[eduDic valueForKey:@"schoolName"]; 


destinationLabel.text=[[[[dics valueForKey:@"positions"] valueForKey:@"values"] objectAtIndex:0] valueForKey:@"title"]; 

NSArray *skillsArray=[[NSArray alloc] initWithArray:[[dics valueForKey:@"skills"]valueForKey:@"values"]]; 


NSString *skillStr=[[NSString alloc] initWithFormat:@""]; 

for (NSDictionary *skillDic in skillsArray) 
{ 
    if (![skillStr isEqualToString:@""]) { 
    skillStr=[skillStr stringByAppendingFormat:@","]; 
    } 
    skillStr=[skillStr stringByAppendingFormat:@"%@",[[skillDic valueForKey:@"skill"]valueForKey:@"name"]]; 
} 

NSLog(@"skill string is %@",skillStr); 

skillsLable.text=[NSString stringWithFormat:@"%@",skillStr]; 

} 
+0

感謝Hercules。我可能會回到這個問題,我希望避免的唯一問題是手動呈現PDF的每個元素。我的觀點有圖像,標籤,文字等...走這條路線意味着我需要重新繪製PDF中的整個佈局,並且按照它們在視圖中看起來的方式對其進行度量/定位...正確? – hhaddad

1

如果你罰款繪製儘可能多的文本頁面上的適合和你有從你的UITextView是在PDF切斷與文字的煩惱,要禁用的UITextView的滾動。無論是在Interface Builder或以編程方式:

textview.scrollEnabled = NO; 

當scrollEnabled設置爲YES,文本得到了我的PDF切斷。如果沒有滾動,我會得到整個文本 - 無論如何至少可以放在一頁上。

如果您需要在多個頁面上生成文本,我認爲您必須求助於將您的元素逐個繪製到PDF繪圖上下文中,如其他答案所示。