gpt4 book ai didi

ios - 在 iPhone 中将 UITextView 转换为 Pdf 文件?

转载 作者:行者123 更新时间:2023-11-28 20:27:47 25 4
gpt4 key购买 nike

我在 iPhone 应用程序中工作,使用 UITextView 从用户那里获取一些内容,然后我尝试从该 UITextView 转换 pdf 文件并存储在本地目录中,但我不知道这一点。

最佳答案

这是代码

- (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.
[self drawText];

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

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


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

NSString *textToDraw = yourtextview.text // use your textfield or textview here

UIFont *font = [UIFont systemFontOfSize:14.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 + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

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

关于ios - 在 iPhone 中将 UITextView 转换为 Pdf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com