本文介绍了将NSAttributedString与NSTextAttachment保存到文件中。如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSTextView ,可能包含富文本或富文本与图像 NSTextAttachment 。我如何添加附件:

I have a NSTextView, which may contains rich text or rich text with image as NSTextAttachment. There is how I adds attachment:

NSImage *image = [NSImage imageNamed:@"image"];

NSTextAttachmentCell *attachmentCell =[[NSTextAttachmentCell alloc] initImageCell:image];
NSTextAttachment *attachment =[[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
NSAttributedString *attributedString =[NSAttributedString  attributedStringWithAttachment: attachment];
[[aTextView textStorage] beginEditing];
if ([aTextView shouldChangeTextInRange:NSMakeRange([aTextView selectedRange].location, 0) replacementString:@""]) {
    [[aTextView textStorage] insertAttributedString:attributedString atIndex:[aTextView selectedRange].location];
    [aTextView didChangeText];
}
[[aTextView textStorage] endEditing];

我的 -fileWrapperOfType:error:

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
    NSRange documentRange = NSMakeRange(0, [[[WindowController aTextView] textStorage] length]);
    NSTextStorage *text = [[WindowController aTextView] textStorage];

    NSFileWrapper *resultWrapper = nil;
    if ([typeName compare:@"public.rtf"] == NSOrderedSame) {
        resultWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[text RTFFromRange:documentRange documentAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NSRTFTextDocumentType, NSDocumentTypeDocumentAttribute, nil]]];
    }
    else if ([typeName compare:@"com.apple.rtfd"] == NSOrderedSame) {
        resultWrapper = [text RTFDFileWrapperFromRange:documentRange documentAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NSRTFDTextDocumentType, NSDocumentTypeDocumentAttribute, nil]];
    }
    return resultWrapper;
}

但是当我保存RTFD时,所有附件丢失。请帮忙。

But when I'm saving RTFD, all attachments looses. Please, help. What I'm missing?

推荐答案

我找到一个可以接受的解决方案,它的描述如下:

I have found an acceptable solution, it's described here: Cocoa: custom attachment in a text view

这篇关于将NSAttributedString与NSTextAttachment保存到文件中。如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 01:02