11

I want to save the attributedString of my UItextview'text. I tried to convert to string and save. But when I back to set the text'attributedString,it not work.

This is how I convert to String:

var a = String(stringInterpolationSegment: text.attributedText)
    Data.setValue(a, forKey: "font")
    Data.managedObjectContext?.save(nil)

This is how I back to set:

text.attributedText = NSAttributedString(string: size)

but my TextView just show the AttributedSting

{
NSFont = "<UICTFont: 0x7fa9fa82aec0> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
NSParagraphStyle = "Alignment 0, 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 (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}

I'm sorry that I can't post images. I use the swift to do this.

My questions: How can i do this ? Or there have another way to save the attributedText?

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
li fubing
  • 113
  • 1
  • 4
  • possible duplicate of [Storing NSAttributedString Core Data](http://stackoverflow.com/questions/4266226/storing-nsattributedstring-core-data) – i_am_jorf May 20 '15 at 01:54
  • @i_am_jorf this question I have been read before,it write by object-c.I don't konw how to do this yet – li fubing May 20 '15 at 02:17
  • Please refer to the answer by pob21 in the link by i_am_jorf. It is not the accepted answer but it is the right way to do it. I do the same in my app (using Swift). – Joe Smith May 20 '15 at 04:20
  • @JoeSmith thanks for your advice! I am sorry to ask a similar question.I just a swift begainner.Can you share your code if you don't mind,thanks – li fubing May 20 '15 at 04:50

3 Answers3

14

The attribute should look like this in the data model.

enter image description here

The header file should be modified to match this:

@property (nonatomic, retain) NSAttributedString * attributedText;

That's it. You should be able to persist your attributed string just like any other attributes.

Suppose your entity is Event, and you have an object event of type Event, you can access it as event.attributedText. Here are some sample Swift code:

event.attributedText = NSAttributedString(string: "Hello World")
let attributedString = event.attributedText

Let us know should you prefer the answer in your native language.

Joe Smith
  • 1,900
  • 1
  • 16
  • 15
  • thanks for your advice! I am confused about how to do the header file you said.I am swift begainner.Hope can be generous with your help.:-) – li fubing May 20 '15 at 16:35
9

In Xcode 10 with automatic code generation this is a lot simpler than the other suggestions.

  1. Open the data model and select the name of the Attribute
  2. Open the Data Model inspector (Command+Option+3)
  3. Set Attribute Type to Transformable
  4. Set Custom Class to NSAttributedString

Screenshot of Xcode attribute inspector

And that's it, now you can just save your data in your Swift code as you'd expect, with no need for modifying generated classes or forced casting, e.g.:

detailItem.content = textView.attributedText

rodhan
  • 633
  • 7
  • 12
0

Yes you can add attributedText with a simple workaround.

Add a attribute of type Transformable. After creating the .h file change the type of that attribute to NSAttributedString. In that attribute you can easily save the attributed string.

Hope this helps.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73