Writing NSMutableAttributedString to rtf file could not open?

649 views Asked by At

My tried code is given below:-

#define FontFamily(fname,fsize) [UIFont fontWithName:fname size:fsize]
#define themeRed [UIColor colorWithRed:189.0f/255.0f green:0.0f/255.0f blue:12.0f/255.0f alpha:1]
#define helveticaBold @"HelveticaNeue-Bold"
#define helveticaReg @"HelveticaNeue"
#define M 16.0f
#define L 20.0f

Code for writing attributedString

 NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/MyUser"];
    NSFileManager *fm = [NSFileManager defaultManager];

    if (![fm fileExistsAtPath:docDir]) {
        [fm createDirectoryAtPath:docDir withIntermediateDirectories:NO attributes:nil error:nil];
    }

    NSString *filepath  = [docDir stringByAppendingPathComponent:@"WRP.rtf"];

    NSString *noteAsAttachment = [freehandTtl.text stringByAppendingString:[NSString stringWithFormat:@"\n%@",freehandTxt.text]];

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:noteAsAttachment];

    [attributedStr addAttribute:NSFontAttributeName value:FontFamily(helveticaBold, L) range:NSMakeRange(0, freehandTtl.text.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:themeRed range:NSMakeRange(0, freehandTtl.text.length)];

    [attributedStr addAttribute:NSFontAttributeName value:FontFamily(helveticaReg, M) range:NSMakeRange(freehandTtl.text.length+1, freehandTxt.text.length)];

    NSLog(@"attr str is %@",attributedStr);

    [attributedStr fileWrapperFromRange:NSMakeRange(0, [attributedStr length])  documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType} error:nil];

    NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] init];

    BOOL success = [fileWrapper writeToURL:[NSURL fileURLWithPath:filepath] options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil];

My String value is given below:--

attr str is My File Title {
    NSColor = "UIDeviceRGBColorSpace 0.741176 0 0.0470588 1";
    NSFont = "<UICTFont: 0xa140f00> font-family: \"Helvetica Neue\"; font-weight: bold; font-style: normal; font-size: 20.00pt";
}
{
}Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.{
    NSFont = "<UICTFont: 0x8ea1fc0> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 16.00pt";
}

and success variable of type BOOL also shows yes , file written successfully. But when I try to open rtf file at my folder location it shows an error message as displayed below:-

enter image description here

2

There are 2 answers

4
trojanfoe On BEST ANSWER

This code doesn't make any sense:

[attributedStr fileWrapperFromRange:NSMakeRange(0, [attributedStr length])
                 documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
                              error:nil];

NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] init];

BOOL success = [fileWrapper writeToURL:[NSURL fileURLWithPath:filepath]
                               options:NSFileWrapperWritingAtomic
                   originalContentsURL:nil
                                 error:nil];

As you are asking the attributed string for a file wrapper and then ignoring what it returns and creating a new one, with no content. This has more chance of success:

NSFileWrapper *fileWrapper = [attributedStr fileWrapperFromRange:NSMakeRange(0, [attributedStr length])
                 documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
                              error:nil];

BOOL success = [fileWrapper writeToURL:[NSURL fileURLWithPath:filepath]
                               options:NSFileWrapperWritingAtomic
                   originalContentsURL:nil
                                 error:nil];
0
Prince Kumar Sharma On

Another way to write RTF.

BOOL success = [self writeAttributeString:attributedStr toPath:filepath];

-(BOOL)writeAttributeString:(NSMutableAttributedString*)attrStr toPath:(NSString*)path
{
    NSData *data = [attrStr dataFromRange:NSMakeRange(0, [attrStr length])
                       documentAttributes:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInteger:1]} error:nil];

    return [data writeToFile:path atomically:YES];
}

Here filePath contains your rtf file path to document directory with rtf extension.