Search code examples
iosuitextviewrtfnsattributedstring

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


I'm trying to load an RTF-File in a UITextView with the following Code:

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

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

With iOS 8 it works perfectly, but on iOS 7 it doesn't work, it only displays the Text without attributes.


Solution

  • I solved it saving the RTF-Files as HTML (with TextEdit) and changed the code to

    NSURL * filePath = [[NSBundle mainBundle] URLForResource:_fileName withExtension:@"html"];
    
    NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:filePath options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    myTextView.attributedText = stringWithHTMLAttributes;