不幸的是,考虑到它们的复杂性,在 Swift 中创建 .docx 文件几乎是不可能的(您可以通过将任何旧的 .docx 文件的文件扩展名更改为 .zip 来亲自查看,这将揭示它们的内部结构)。下一个最好的事情是简单地创建一个.txt 文件,该文件也可以在 Pages 中打开(但遗憾的是不是 Docs)。如果您正在寻找更精致的格式,包括格式甚至图像,您可以选择创建一个.pdf 文件。
以下是一些可能有帮助的代码示例:
在 Swift 3 中创建和共享 .txt 文件:
func export(_ string: String, title: String) throws {
// create a file path in a temporary directory
let fileName = "\(title).txt"
let filePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(fileName)
// save the string to the file
try string.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
// open share dialog
// Initialize Document Interaction Controller
self.interactionController = UIDocumentInteractionController(url: URL(fileURLWithPath: filePath))
// Configure Document Interaction Controller
self.interactionController!.delegate = self
// Present Open In Menu
self.interactionController!.presentOptionsMenu(from: yourexportbarbuttonoutlet, animated: true) // create an outlet from an Export bar button outlet, then use it as the `from` argument
}
这可以调用
export("Hello World", title: "HelloWorld")
立即创建一个 txt 文件并为其打开共享对话框。
在 Swift 3 中创建和共享一个简单的 .pdf 文件:
func openPDF(_ string: String, title: String) throws {
// 1. Create a print formatter
let html = "<h2>\(title)</h2><br><h4>\(string)</h4>" // create some text as the body of the PDF with html.
let fmt = UIMarkupTextPrintFormatter(markupText: html)
// 2. Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAt: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 10, y: 10, width: 595.2, height: 841.8) // A4, 72 dpi, x and y are horizontal and vertical margins
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 pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil)
for i in 1...render.numberOfPages {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPage(at: i - 1, in: bounds)
}
UIGraphicsEndPDFContext();
// 5. Save PDF file
var path = "\(NSTemporaryDirectory())\(title).pdf"
pdfData.write(toFile: path, atomically: true)
print("open \(path)") // check if we got the path right.
// open share dialog
print("opening share dialog")
// Initialize Document Interaction Controller
self.interactionController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
// Configure Document Interaction Controller
self.interactionController!.delegate = self
// Present Open In Menu
self.interactionController!.presentOptionsMenu(from: yourexportbarbuttonoutlet, animated: true) // create an outlet from an Export bar button outlet, then use it as the `from` argument
}
这可以用
调用
openPDF("Hello World", title: "HelloWorld")
立即创建一个 pdf 文件并为其打开共享对话框。
编辑: 找到了一个有趣的(虽然不是经过完善的)解决方法来在 Google 文档中打开文本:使用此处“创建 .txt 文件”部分中的函数,然后进行更改"\(title).docx" 的文件名。这会使 Docs 误以为这是一个 .docx 文档,这将允许文本在 Docs 中成功打开。不幸的是,这会创建一个无法由 Pages、Word 或任何其他应用程序打开的无效文档,因为它实际上并没有创建一个真正的文档文件。并且交互控制器会让用户看起来好像他们也可以在 Pages 中打开它,尽管这总是失败。