objective-c – 将带有NSTextAttachment的NSAttributedString保存到文件中.如何?

前端之家收集整理的这篇文章主要介绍了objective-c – 将带有NSTextAttachment的NSAttributedString保存到文件中.如何?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个NSTextView,它可能包含富文本或富文本,图像为NSTextAttachment.有我如何添加附件:

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:错误方法

- (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,nil]];
    }
    return resultWrapper;
}

但是当我保存RTFD时,所有附件都会丢失.请帮忙.我错过了什么?

解决方法

我找到了一个可接受的解决方案,它在这里描述: Cocoa: custom attachment in a text view

猜你在找的cocoa相关文章