如何将attributionText UITextView保存到数据库

时间:2017-11-09 06:35:16

标签: ios objective-c realm uitextview nsattributedstring

attributionText数据结构如下

S{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fd82c717bc0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x7fd82c717bc0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 2.90954, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 1, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}hepi{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fd82c717bc0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 2.90954, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 1, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}

我想保存它,当用户重新打开应用程序时,可以在UITextView中再次看到这些(不会丢失颜色,字体大小等字体参数)

我尝试将它作为String保存到数据库中,但我不能将它用于UITextView的attributedText

此外,它可能包含UITextView中的图片

我使用的数据库是Realm有没有办法实现它?

如果可以,请采用上述数据结构进行最佳

演示

1 个答案:

答案 0 :(得分:1)

您可以使用-[NSAttributedString dataFromRange:documentAttributes:error:]NSAttributedString转换为可以存储在Realm中的NSData。然后,您可以使用-[NSAttributedString initWithData:options:documentAttributes:error:]向反方向前进,从NSData返回NSAttributedString