Skip to content

Latest commit

 

History

History
59 lines (37 loc) · 2.85 KB

Readme.md

File metadata and controls

59 lines (37 loc) · 2.85 KB

What's all this, then?

This is an example project demonstrating an NSTextView subclass which does programmatic modification of if its contents based on user actions. This would be useful, for example, in a programmer's text editor, whereby it could automatically insert close braces when one types an open brace, or automatically indent when one presses return.

Why?

Because I needed this functionality for a project I'm writing. I faced several challenges:

  1. I had been calling insertText: to perform programmatic changes, and although this appeared to work, I was troubled by some verbiage in NSTextView's documentation for insertText:

    This method is the entry point for inserting text typed by the user and is generally not suitable for other purposes. Programmatic modification of the text is best done by operating on the text storage directly.

    Problem with that is, modifying text by directly manipulating the text storage completely bypasses Undo. I think the proper answer is to do you own undo handling in your text storage, but I did not want to do this.

    I posted this question to stackoverflow, without much in the way of an answer.

  2. I had issues getting Undo to work correctly, and I was apparently not alone, as witnessed by this question on stackoverflow.

So the point of this is to demonstrate an NSTextView subclass which does programmatic modification of its contents using insertText: and without screwing up Undo.

How's it work?

ZPTextView is an NSTextView subclass. It overrides insertText:, and does it's modification of its contents there. Additionally, it does its own undo grouping in insertText:, basicallly by creating an undo group for each insertText: call. Additionally, it overrides replaceCharactersInRange: to also call shouldChangeTextInRange:, also to keep undo working properly.

Note that this is code in progress. It's basically a cut-down version of code I'm using in a project I'm working on, and I've not actually shipped it (yet). So far, however, it appears to work well.

Who's responsible for this?

I'm Zacharias Pasternack, lead developer for Fat Apps, LLC. You can check out my blog, or follow me on Twitter or App.net.

Can I use this code?

You bet. Do whatever you want with it. If you find issues, please let me know. If you make it better, please let me know.

License

The code is available under a Modified BSD License. See the LICENSE file for more info.