I need to add a custom attribute to the selected text in an NSTextView. So I can do that by getting the attributed string for the selection, adding a custom attribute to it, and then replacing the selection with my new attributed string.
So now I get the text view’s attributed string as NSData and write it to a file. Later when I open that file and restore it to the text view my custom attributes are gone! After working out the entire scheme for my custom attribute I find that custom attributes are not saved for you. Look at the IMPORTANT note here: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/AttributedStrings/Tasks/RTFAndAttrStrings.html
So I have no idea how to save and restore my documents with this custom attribute. Any help?
The normal way of saving an
NSAttributedStringis to use RTF, and RTF data is what the-dataFromRange:documentAttributes:error:method ofNSAttributedStringgenerates.However, the RTF format has no support for custom attributes. Instead, you should use the
NSCodingprotocol to archive your attributed string, which will preserve the custom attributes: