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:

  1. The first stage is to send it a beginEditing message to announce a group of changes.

  2. In the second stage, you send it some editing messages, such as deleteCharactersInRange: and 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 beginEditing message.

  3. For the third stage, when you’re done changing the text storage object, you send it the endEditing message. This causes it to invoke its own processEditing method, 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.