我的方案是,我尝试保存三种格式的.pdf,.doc和.txt的UITextView文本。在此,甲骨文选项用户可以根据警报选项进行选择。一旦保存,需要在预览控制器中显示以进行文件共享。如何实现呢?
func createPDF(text:String, filename:String) {
// 1. Create Print Formatter with input text.
let formatter = UIMarkupTextPrintFormatter(markupText: text)
// 2. Add formatter with pageRender
let render = UIPrintPageRenderer()
render.addPrintFormatter(formatter, startingAtPageAt: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
let printable = page.insetBy(dx: 0, dy: 0)
render.setValue(NSValue(cgRect: page), forKey: "paperRect")
render.setValue(NSValue(cgRect: printable), forKey: "printableRect")
// 4. Create PDF context and draw
let rect = CGRect.zero
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, rect, nil)
for i in 1...render.numberOfPages {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPage(at: i - 1, in: bounds)
}
UIGraphicsEndPDFContext();
// 5. Save PDF file
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
pdfData.write(toFile: "\(documentsPath)/\(filename).pdf", atomically: true)
print("saved success:\(documentsPath)\(filename)")
listFiles()
}
最佳答案
如果我理解正确,则需要出示文件
您可以使用QLPreviewController
完成此操作
更多信息-
https://developer.apple.com/documentation/quicklook
https://developer.apple.com/documentation/quicklook/qlpreviewcontroller#//apple_ref/occ/cl/QLPreviewController
https://medium.com/@garg.vivek/quick-display-documents-with-qlpreviewcontroller-eaca97928c7aQLPreviewController
具有共享控制权
关于ios - 快速将UITextView文本保存到.Pdf,.Doc和.Txt文件以进行格式和显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56849245/