【问题标题】:Can NSAttributedString which contains NSTextAttachment be stored(or restored)?可以存储(或恢复)包含 NSTextAttachment 的 NSAttributedString 吗?
【发布时间】:2016-04-07 02:38:55
【问题描述】:

我正在编写一个文本编辑器,用户可以使用它输入带有样式的文本并插入图像,所以我使用 NSAttributedString 来管理内容。我的问题是如何存储文本编辑器关闭之前的内容,并在下次打开编辑器后恢复内容?

我写了一个 NSAttributedString 类别,现在我可以在 NSTextAttachment 中存储(和恢复)文本但不能存储 UIImageView,下面是我的代码的一部分:

-(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;

}

我花了相当长的时间进行调查和实验,搜索显示有几个人有相同的问题,但没有令人满意的答案。

【问题讨论】:

    标签: ios serialization nsattributedstring rich-text-editor nstextattachment


    【解决方案1】:

    最后,我使用了一个名为YYTextArchiver 的 NSKeyedArchiver 子类来直接序列化 NSAttributedString,它对我有用。

    【讨论】:

      【解决方案2】:
      //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

      【讨论】:

        【解决方案3】:

        fileWrapperFromRange:documentAttributes:error:NSRTFDTextDocumentType 的文档类型一起使用。

        【讨论】:

        • 我不熟悉 NSFileWrapper,你能提供更多细节或任何链接吗?谢谢。
        猜你喜欢
        • 2012-05-19
        • 2015-02-01
        • 2011-03-18
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2012-06-19
        • 2012-06-27
        • 2020-08-11
        相关资源
        最近更新 更多