簡體   English   中英

可以存儲(或恢復)包含 NSTextAttachment 的 NSAttributedString 嗎?

[英]Can NSAttributedString which contains NSTextAttachment be stored(or restored)?

我正在編寫一個文本編輯器,用戶可以使用它輸入帶有樣式的文本並插入圖像,所以我使用 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;

}

我花了相當多的時間進行調查和實驗,搜索顯示有幾個人有同樣的問題,但沒有令人滿意的答案。

最后,我使用了一個名為YYTextArchiver的 NSKeyedArchiver 子類來直接序列化 NSAttributedString,它對我有用。

//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)
        }

演示

使用fileWrapperFromRange:documentAttributes:error:文檔類型為NSRTFDTextDocumentType

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM