swift - NSAttributedString dataFromRange 生成没有任何尾部缩进的 docx 文件

标签 swift cocoa cocoa-touch nsattributedstring

这是我在 NSAttributedString 类中发现的奇怪行为。根据文档,此类的对象可以产生各种类型的数据。我想将文本转换为 docx 现代格式。再次根据文档,我应该使用 NSOfficeOpenXMLTextDocumentType 文档属性。所以我这样做:

// produce an NSAttributedString with all of the indents have set
let stringToConvert = currentDocument.computePrintableAttributedString(forProduct: product)`

// now create a document attributes dictionary
let documentAttributes = [NSDocumentTypeDocumentAttribute : NSOfficeOpenXMLTextDocumentType]

// finally convert the string into the docx data
do {
    let convertedData = try stringToConvert.dataFromRange(fullLength, documentAttributes: documentAttributes)
} catch {
    print(error)
}

一切正常。我将数据写入文件。并在 MS Word 中打开它。一切看起来都很好...除了文档丢失了属性字符串段落样式的所有尾部缩进。

我做错了什么?如何在导出的docx文档中保留尾部缩进?

我尝试了 NRTFTextDocumentType 属性,并且得到了一个格式非常良好的 rtf 文件,其中所有尾部缩进都已就位。

我对 NSOfficeOpenXMLTextDocumentType 缺少什么?

最佳答案

您似乎发现了一个错误/功能。我可以使用在 OS X 10.11.4 下运行的 TextEdit 1.11 重现该行为,并且 TextEdit 在内部使用 NSAttributedString 。保存为 rtf 时,尾部缩进和首行缩进均保留;当另存为 Word 97、2003 或 2007 时,如果在 TextEdit 中重新打开文件,尾部缩进将会丢失;如果在第 5 页中打开文件,第一行缩进也会丢失。 LibreOffice 在读取所有三个时会丢失尾部缩进,在读取 2003 或 2007 时会丢失首行缩进,在读取 2003 时会丢失对齐。在 PC Word 2013 上,读取所有三个时会丢失尾部缩进,而在读取 2007 时会丢失首行缩进。

建议您通过 bug report.apple.com 向 Apple 报告该问题

关于swift - NSAttributedString dataFromRange 生成没有任何尾部缩进的 docx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380632/

相关文章:

ios - 动态调整单元格大小时如何获取 UITableView 的高度?

ios - 为什么我的 Swift for-in-loop 不执行/迭代?

cocoa - 使 OSX 应用程序在未获得焦点时响应第一次鼠标单击

iPhone SDK : Delegate method not getting called-Troubleshooting

ios - 如何为 CALayer 使用基于 block 的动画?

swift - 无法将类型 'AVPlayer' 的值转换为 'Swift.AnyObject.Type'

objective-c - 在哪里可以找到实现 NSTextField 掩码的 NSFormatter 示例

objective-c - 连续的 'if' 语句

iPhone:使用 UIViewController 以节省内存

swift - 如何隐藏移动键盘的上栏?