Changing Text Storage
The behavior of an
NSTextStorage object is best illustrated by following the messages you send to change its text.There are three stages to editing a text storage object programmatically:
The first stage is to send it a
beginEditingmessage to announce a group of changes.
In the second stage, you send it some editing messages, such as
addAttributes:range:, to effect the changes in characters or attributes. Each time you send such a message, the text storage object invokes
edited:range:changeInLength:to track the range of its characters affected since it received the
For the third stage, when you’re done changing the text storage object, you send it the
endEditingmessage. This causes it to invoke its own
processEditingmethod, fixing attributes within the recorded range of changed characters. (See Text Attribute Programming Topics for information about attribute fixing.)
After fixing its attributes, the text storage object sends a message to each associated layout manager indicating the range in the text storage object that has changed, along with the nature of those changes. The layout managers in turn use this information to recalculate their glyph locations and redisplay if necessary.
NSTextStorage also keeps a delegate and sends it messages before and after processing edits.