【问题标题】:How to save the attributedText UITextView to the database如何将属性化文本 UITextView 保存到数据库
【发布时间】:2017-11-09 06:35:15
【问题描述】:

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,这样最好

【问题讨论】:

    标签: ios objective-c realm uitextview nsattributedstring


    【解决方案1】:

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

    【讨论】:

    • 这个主意不错,我现在尝试实现。另外,如果有图片(NSTextAttachment),转换成Data可以保存吗?
    • 如果您选择使用支持图像附件的格式,例如 RTFD,那么是的,您应该能够往返此类附件。
    • 感谢您的帮助,我根据您的建议解决了问题
    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2013-12-15
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多