OGeek|极客世界-中国程序员成长平台

标题: ios - 可以存储(或恢复)包含 NSTextAttachment 的 NSAttributedString 吗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:50
标题: ios - 可以存储(或恢复)包含 NSTextAttachment 的 NSAttributedString 吗?

我正在编写一个文本编辑器,用户可以使用它输入带有样式的文本并插入图像,所以我使用 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 arrayWithObjectsNSString*)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;

}

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



Best Answer-推荐答案


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

关于ios - 可以存储(或恢复)包含 NSTextAttachment 的 NSAttributedString 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465761/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4