About how to storage NSAttributedString with sqlite3 and how to convert it

I used sqlite3 blob type to save NSAttributedString content, query table return NSData type and use below code to convert it to NSAttributedString :


let attributeStr = try? NSAttributedString.init(data: data as Data, options: [:], documentAttributes: nil)
textView.attributedText = attributeStr


But the UITextView show this, how to do about this, thanks a lot.

sorry, the image not show.

just describe :

I build a rich text demo with UITextView, get NSAttributedString and save it in db, then query table to get it and show, I edited "Tttt\nRruuoo" on the page, but the UITextView show below: (it's not in console)


T{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = " font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 22.00pt";
    NSOriginalFont = " font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, 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 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";
}ttt{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = " font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 22.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, 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 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";
}
\
{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = " font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, 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 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";
}{
    NSFont = " font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

Images do not show on forum.


Could you explain what you get ?

I get this


T{

NSColor = "UIExtendedGrayColorSpace 0 1";

NSFont = "<UICTFont: 0x7ff266520700> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 22.00pt";

NSOriginalFont = "<UICTFont: 0x7ff266509e40> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";

NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, 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 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";

}ttt{

NSColor = "UIExtendedGrayColorSpace 0 1";

NSFont = "<UICTFont: 0x7ff266520700> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 22.00pt";

NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, 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 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";

}

Rruuoo

{

NSColor = "UIExtendedGrayColorSpace 0 1";

NSFont = "<UICTFont: 0x7ff26672fa20> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";

NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, 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 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";

}{

NSFont = "<UICTFont: 0x7fec72f0c810> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 12.00pt";

}

Please explain.


Is it the print of attributeStr ? Or something else ?

It is quite normal as an output of `print` of an `NSAttributedString`, what do you think is the problem with it?

show this on simulator, it's not log info.

the simulator show it, not on console

Do you mean that the string is shown in the textVeiw on the simulator running your app?

So, you should show how you get data.

yes, I used UITextView to create a rich text demo, used NSAttributedString add some attributes, it's done well, but I want to save the data with attributes. so I used sqlite3 blob to storage the data, query data from table return NSData, then I used the above code to convert NSData to NSAttributedString. run the app, the string is shown in UITextView on the simulator. so How to solve it? thans a lot.

I used below code to get data from db.


func queryTable() -> [NSData] {

var stmt:OpaquePointer? = nil

let sql = "SELECT note_content FROM note_table"

let csql = (sql.cString(using: String.Encoding.utf8))

var rows = [NSData]()

if sqlite3_prepare(db, csql, -1, &stmt, nil) == SQLITE_OK{

while sqlite3_step(stmt) == SQLITE_ROW {

let data = sqlite3_column_blob(stmt, 0)

let size = sqlite3_column_bytes(stmt, 0)

let val = NSData(bytes: data!, length: Int(size))

rows.append(val)

}

}else{

print("SQLITE_ERROR")

}

return rows

}

Can you show your code to create a Data from your NSAttributedString ?

thanks a lot for your reply, I find the bug, wrong write type when insert into db. now the problem is solved.

Can you please take some time to show the right code?


It is useful for future readers of this thread, and a good manner for who spent some time to write replies.

About how to storage NSAttributedString with sqlite3 and how to convert it
 
 
Q