如何将 attributedText UITextView 保存到数据库

How to save the attributedText UITextView to the database

attributedText数据结构如下

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中再次看到这些(不要丢失颜色,字体大小等字体参数)

我尝试将其作为字符串保存到数据库中,但我无法将其用于 UITextView 的属性文本

另外在UITextView中可能包含图片

我用的数据库是Realm 有什么办法可以实现吗?

可以的话拿上面的数据结构做个demo最好

您可以使用 -[NSAttributedString dataFromRange:documentAttributes:error:] to convert your NSAttributedString into an NSData that you can store in Realm. You can then use -[NSAttributedString initWithData:options:documentAttributes:error:] 返回相反的方向,从 NSData 回到 NSAttributedString