【问题标题】:Saving NSAttributedString with NSTextAttachment into file. How to?将带有 NSTextAttachment 的 NSAttributedString 保存到文件中。如何?
【发布时间】:2012-06-27 15:05:46
【问题描述】:

我有一个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: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 时,所有附件都会丢失。请帮忙。我错过了什么?

【问题讨论】:

    标签: objective-c cocoa nstextview nsattributedstring nstextattachment


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 2012-02-26
      • 1970-01-01
      • 2015-02-01
      • 2017-04-15
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多