Ethan Mick
Ethan Mick

Reputation: 9567

Load NSAttributedString from File

Is there a way to have some sort of rich text in a file, and load the file into an NSAttributedString? I looked into loading a .rtf file into an NSAttributed String:

NSString *filePathFooter = [[NSBundle mainBundle] pathForResource:kFooterFileName ofType:@"rtf"];

NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:[NSURL URLWithString:filePathFooter]
                                                                 options:nil
                                                      documentAttributes:NULL
                                                                   error:&error];

But this leaves me with:

Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)"

Is there a way to do this?

Upvotes: 1

Views: 1526

Answers (2)

ozz
ozz

Reputation: 1137

Use URLForResource instead, and pass the resulting URL like so:

NSURL *url = [[NSBundle mainBundle] URLForResource:kFooterFileName withExtension:@"rtf"];

NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:url
                                                                 options:nil
                                                      documentAttributes:NULL
                                                                   error:&error];

Upvotes: 4

Greg
Greg

Reputation: 33650

NSCocoaErrorDomain error code 258 is NSFileReadInvalidFileNameError, so it looks like it can't find the file you're passing.

Upvotes: 0

Related Questions