1

有没有办法在文件中包含某种富文本,并将文件加载到 NSAttributedString 中?我研究了将 .rtf 文件加载到 NSAttributed 字符串中:

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];

但这给我留下了:

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

有没有办法做到这一点?

4

2 回答 2

4

改为使用URLForResource,并像这样传递生成的 URL:

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

NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:url
                                                                 options:nil
                                                      documentAttributes:NULL
                                                                   error:&error];
于 2013-11-08T03:49:39.040 回答
0

NSCocoaErrorDomain错误代码 258 是NSFileReadInvalidFileNameError,所以看起来它找不到您传递的文件。

于 2013-09-30T23:37:35.217 回答