iOS 写Word第三方库
pod 'DocX-Swift'
https://github.com/shinjukunian/DocX
用法
let string = NSAttributedString(string: "This is a string", attributes: [.font: UIFont.systemFont(ofSize: UIFont.systemFontSize), .backgroundColor: UIColor.blue])
let url = URL(fileURLWithPath:"myPath")
try? string.writeDocX(to: url)
从iOS 15/macOS 12开始,您可以使用新的AttributedString。
var att=AttributedString("Lorem ipsum dolor sit amet")
att.font = NSFont(name: "Helvetica", size: 12)
att[att.range(of: "Lorem")!].backgroundColor = .blue
let url = URL(fileURLWithPath:"myPath")
try att.writeDocX(to: url)
当然,这也适用于Markdown:
let mD="~~This~~ is a **Markdown** *string*."
let att=try AttributedString(markdown: mD)
try att.writeDocX(to: url)
DocXOptions允许自定义DocX输出。
您可以选择使用DocXOptions指定元数据:
let font = NSFont(name: "Helvetica", size: 13)! //on macOS
let string = NSAttributedString(string: "The Foundation For Law and Government favours Helvetica.", attributes: [.font: font])
var options = DocXOptions()
options.author = "Michael Knight"
options.title = "Helvetica Document"
let url = URL(fileURLWithPath:"myPath")
try string.writeDocX(to: url, options:options)