我有一些输出是一个非常简单的 RTF 文件。当我生成此文档时,用户可以通过电子邮件发送它。这一切都很好。文档看起来不错。获得 NSAttributedString 后,我创建一个 NSData 块,并将其写入文件,如下所示:
NSData* rtfData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:&error];
该文件可以通过电子邮件发送。当我检查电子邮件时,一切都很好。
现在,我的任务是在文档顶部添加一个 UIImage。太好了,所以我正在创建一个这样的属性字符串:
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
UIImage* image = [UIImage imageNamed:@"logo"];
attachment.image = image;
attachment.bounds = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
NSMutableAttributedString *imageAttrString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
// sets the paragraph styling of the text attachment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter]; // centers image horizontally
[paragraphStyle setParagraphSpacing:10.0f]; // adds some padding between the image and the following section
[imageAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [imageAttrString length])];
[imageAttrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
此时,在 Xcode 中,我可以对 imageAttrString 进行 QuickLook,它绘制得很好。
构建此字符串后,我将执行以下操作:
[attrString appendAttributedString:imageAttrString];
然后添加我最初生成的所有其他属性文本。
当我现在查看文件时,没有图像。QuickLook 在调试器中看起来不错,但最终输出中没有图像。
提前感谢您对此的任何帮助。