When using a UITextView I try to add a custom attribute to an attributed string. However, all custom keys are lost after assigning to a UITextView\'s attributedText. Like this:<
Based on the comments, you are trying to misuse NSAttributedString
.NSAttributedString
and UITextView
are only designed to handle the documented attributes. You may be able to initially store custom attributes in the string, but once the text view starts processing the attributed text and you ask the text view for the latest attributed text, the text view will have long gotten rid of any custom attributes.
The better solution would be to create a class that extends UITextView. Your subclass should add a property that can hold whatever custom attributes you want. Or maybe your custom class would override the attributedText
and 'setAttributedText:` methods. These would take care of saving off and restoring any custom attributes found in the attributed string.
Note: this answer applies to iOS 6 and earlier. For iOS 7 see @julien_c's answer below.