Read RTFD data in IOS

2019-06-09 21:02发布

问题:

I have RTFD data in NSData objects in IOS that I need to read. In MacOSX I just used to read them with NSAttributedString, but now I know that they are not supported in IOS.

 attribString = [[NSAttributedString alloc] initWithRTFD:note documentAttributes:&dict];

Is there any way to read only the text of the RTFD data in IOS?

回答1:

The rtfd in iOS is something like as a directory. If you only want to get the text part, you can try:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"photo" ofType:@"rtfd"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *content = [fileManager contentsOfDirectoryAtPath:filePath error:nil];
NSString *textFilePath = [filePath stringByAppendingPathComponent:@"TXT.rtf"];//you can get the path component from the content; usually it is TXT.rtf
NSError *error = nil;
NSString *pureText = [[NSString alloc] initWithContentsOfFile:textFilePath encoding:NSASCIIStringEncoding error:&error];
NSLog(@"pureText:\n%@",pureText);

Hope it can help.