2

私はテキストエディタを作成しています。ユーザーはスタイルを入力して画像を挿入できるので、NSAttributedStringを使用してコンテンツを管理しています。私の質問は、テキストエディタが閉じられる前に内容を保存し、次回開いたエディターの後に内容を復元する方法です。NSTextAttachmentを含むNSAttributedStringを格納(または復元)できますか?

私は下記、テキストではなくNSTextAttachmentでUIImageView今私は保存(および復元)することができ、NSAttributedStringのカテゴリを書いたが、私のコードの一部です:

-(NSData*)customEncode { 
__block NSMutableArray* archivableAttributes=[[NSMutableArray alloc]init]; 

[self enumerateAttributesInRange:NSMakeRange(0, [self length]) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { 
    NSLog(@"range: %d %d",range.location, range.length); 
    NSLog(@"dict: %@",attrs); 
    NSLog(@"keys: %@", [attrs allKeys]); 
    NSLog(@"values: %@", [attrs allValues]); 

    NSMutableDictionary* tDict=[[NSMutableDictionary alloc]init]; 

    [tDict setObject:[NSNumber numberWithInt:range.location] forKey:@"location"]; 
    [tDict setObject:[NSNumber numberWithInt:range.length] forKey:@"length"]; 

    for (NSString* tKey in [attrs allKeys]) { 
     if ([tKey isEqualToString:@"CTUnderlineColor"]) { 
      [tDict setObject:[NSAttributedString arrayFromCGColorComponents:((CGColorRef)[attrs objectForKey:@"CTUnderlineColor"])] forKey:@"CTUnderlineColor"]; 
     } 
     if ([tKey isEqualToString:@"NSUnderline"]) { 
      NSNumber* underline=[attrs objectForKey:@"NSUnderline"]; 
      [tDict setObject:underline forKey:@"NSUnderline"]; 
     } 
     if ([tKey isEqualToString:@"CTForegroundColor"]) { 
      [tDict setObject:[NSAttributedString arrayFromCGColorComponents:((CGColorRef)[attrs objectForKey:@"CTForegroundColor"])] forKey:@"CTForegroundColor"]; 
     } 
     if ([tKey isEqualToString:@"NSFont"]) { 
      CTFontRef font=((__bridge CTFontRef)[attrs objectForKey:@"NSFont"]); 

      NSDictionary* fontDict=[NSDictionary 
            dictionaryWithObjects: 
            [NSArray arrayWithObjects:(NSString*)CFBridgingRelease(CTFontCopyPostScriptName(font)),[NSNumber numberWithFloat:CTFontGetSize(font)], nil] 
            forKeys: 
            [NSArray arrayWithObjects:@"fontName", @"fontSize", nil]]; 

      [tDict setObject:fontDict forKey:@"NSFont"]; 
     } 
    } 

    [archivableAttributes addObject:tDict]; 
}]; 

NSMutableDictionary* archiveNSMString=[NSMutableDictionary 
             dictionaryWithObjects: [NSArray arrayWithObjects:[self string],archivableAttributes,nil] 
             forKeys:[NSArray arrayWithObjects:@"string",@"attributes",nil]]; 

NSLog(@"archivableAttributes array: %@",archiveNSMString); 

NSData* tData=[NSKeyedArchiver archivedDataWithRootObject:archiveNSMString]; 

NSLog(@"tdata: %@",tData); 

return tData; 

}

私はかなり過ごしましたいくつかの時間を調査して実験し、検索したところ、同じ質問をした人がいたが、満足のいく回答はなかった。

答えて

1

最後に、私はYYTextArchiverという名前のNSKeyedArchiverのサブクラスを使ってNSAttributedStringを直接シリアル化してくれました。

0

文書タイプがNSRTFDTextDocumentTypefileWrapperFromRange:documentAttributes:error:を使用してください。

+0

あなたはより多くの細部または任意のリンクを提供することができます私は、NSFileWrapperに慣れていないのですか?ありがとうございました。 –

0
//Swift-3 
You can store NSTextAttachment images by :- 

var imageArray : [UIImage] 


//MARKS:- Extract attachedImage 
    func textViewDidChange(_ textView: UITextView) { 
     imageArray = [UIImage]() 
     let range = NSRange(location: 0, length: textView.attributedText.length) 
     if (textView.textStorage.containsAttachments(in: range)) { 
      let attrString = textView.attributedText 
      var location = 0 
      while location < range.length { 
       var r = NSRange() 
       let attrDictionary = attrString?.attributes(at: location, effectiveRange: &r) 
       if attrDictionary != nil { 
        let attachment = attrDictionary![NSAttachmentAttributeName] as? NSTextAttachment 
        if attachment != nil { 
         if attachment!.image != nil { 
          //store the image Attached to it. 
          imageArray.append(attachment!.image!) 
         } 
        } 
        location += r.length 
       } 
      } 
     } 
    } 

} 

、連結方式、それに接続されているすべての上記の画像: -

let axtractedImageAttribute = NSMutableAttributedString() 
     for image in imageArray { 
      let attachment:NSTextAttachment = NSTextAttachment() 
      attachment.image = image 
      attachment.setImageHeight(height: 20) 
      let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment) 
      axtractedImageAttribute.append(attachmentString) 
     } 

Demo

関連する問題