gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:34 25 4
gpt4 key购买 nike

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

最佳答案

我找到了一个可接受的解决方案,在此处进行了描述:Cocoa: custom attachment in a text view

关于objective-c - 将带有 NSTextAttachment 的 NSAttributedString 保存到文件中。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com