RTF 中的 NSAttributedString 仅适用于 iOS 8,不适用于 iOS 7

NSAttributedString from RTF works only with iOS 8, not with iOS 7

我正在尝试使用以下代码在 UITextView 中加载 RTF 文件:

NSURL * filePath = [[NSBundle mainBundle] URLForResource:_fileName withExtension:@"rtf"];

NSAttributedString *stringWithRTFAttributes = [[NSAttributedString alloc] initWithFileURL:filePath options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
myTextView.attributedText = stringWithRTFAttributes;

在 iOS 8 上它完美地工作,但在 iOS 7 上它不起作用,它只显示没有属性的文本。

我解决了将 RTF 文件保存为 HTML(使用 TextEdit)并将代码更改为

NSURL * filePath = [[NSBundle mainBundle] URLForResource:_fileName withExtension:@"html"];

NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:filePath options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
myTextView.attributedText = stringWithHTMLAttributes;