views:

187

answers:

1

I'm trying to build an NSTextView that can take "marked up" input that is automatically translated into beautiful RTF-style text while the user types.

The idea is to let the user enter text in "plain text" format, but to "beautify" it on the spot, e.g.

H1 A quick list:
* first item
* second item

would be translated into a first line with a header font, followed by a bulleted list.

I have found plenty of potential ways of doing this, but the Text System is incredibly complicated (with reason) and I don't want to start "cooking my own" if there is already something suitable built-in. BTW I would be happy with a Snow Leopard only API.

The first thing I thought of was "data detectors", but I can't find a public API for doing this.

Having reached the end of the road with that, I turned to the new "Text Input Sources API". This does all kinds of things, but the "data-driven input methods" section of the WWDC 2006 presentation "Take Charge of the Text Input" seems interesting in my context. Beyond that single presentation slide however nothing seems to exist anywhere, so it's a bit of a dead end again.

Finally, I had a look at the NSSpellChecker class which is also supposed to offer completion features and automatic corrections.. but I'm not sure how this could be re-purposed for my requirements either.

At the moment, I'm tempted to just re-parse the entire NSTextStorage manually and make the changes myself when the user stops typing.. but I'm sure there are cleverer heads around this forum..

Any advice or pointers in the right direction would be greatly appreciated.

A: 

Neither data detectors nor the spell checker are appropriate for this task. Assuming you're just looking for a way to pass the input to a parser/formatter you already have, interfacing with the text system isn't too difficult. You're on the right track with handling the editing to NSTextStorage.

Along those lines, there's no need to re-parse the entire thing when the user stops. The text system sends you the modified range and gives you the opportunity to act on those changes (and even reject them out of hand). Since all changes funnel through this (typing, pasting, dropping...), this is the point where you want to intercede.

Because you're dealing with headings and bulleted lists, I'd get the enclosing paragraph of the modified range. This gives you a nice, round unit of work that is easily discovered and perfectly fits what you're trying to accomplish.

Good luck!

Joshua Nozzi
Thanks. I've been playing a bit with the NSTextDelegate and it works but is fairly slow at the moment because I'm using the textDidChange: notification at the moment and I re-parse the entire thing each time..
Frank R.
Not parsing the whole thing each time the user types will definitely help improve performance. :-) The slightest pause in typing can / usually does trigger -textDidChange: so you'll want your parsing/styling code to execute as efficiently as you can possibly manage.
Joshua Nozzi