SPL Tricks: Powerful Multi-Purpose Text Highlighter

One of the things I put a lot of work into for Spelling Plus Library was architecting it in a very modular, extendable fashion. The main reason for this was to allow developers to use it in the way that makes most sense for their projects. For example, Buzzword only uses the SpellingDictionary class, which handles all of the spell checking and spelling suggestion logic, and the WordListLoader, but manages the user interface themselves.

A secondary benefit is that many of the classes are very useful on their own. The best example of this is the TextHighlighter class that forms the core of the SPL SpellingHighlighter. It is a very powerful class for finding and highlighting words in a text field in real time.

It includes a ton of features, including support for manual selections, multi-line highlights, smart targeting, autoupdating on change and scroll, interactive highlights, and more. It will be gaining even more functionality in the upcoming 1.1 release of SPL.

Here is a simple demo of the TextHighlighter class in action. Less than 10 lines of code. It shows how you could use TextHighlighter to search for and highlight words in a text field. You can edit and scroll the text area, and the highlights will update in real time.

This sample will be included with the 1.1 update of SPL.

I’ve considered licensing TextHighlighter separately from SPL, but I’m not sure if anyone would be interested. Let me know if you are.

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

8 Comments

  1. Something like this (maybe not as feature packed though) was also made by Jack Doyle.

    http://blog.greensock.com/category/text/

    Looks good.

  2. So simple, yet so useful. I love tools like these, good job!

  3. One might also consider the Highlighter class in FlexLib, which is pretty darn good too. Just sayin’. 😉

    http://flexlib.googlecode.com/svn/trunk/examples/Highlighter/Highlighter_Sample.swf

  4. Tom – cool.Highlighter looks like a great class, though thankfully it doesn’t do everything I need for SPL (otherwise I’d feel silly for having reinvented the wheel). Guess it answers my question though, probably no need to license TextHighlighter separately. 🙂

    Ha, just noticed they have a fire component too.

  5. Very Cool Grant, can your library be manipulated to edit other things, like if I wanted to check code validation, like an AS Editor would? Is this possible with any features of SPL?

  6. When you scroll the content, the highlight backgrounds update a bit late, a minor quirk tho! Love all your stuff, Flash has no limits!

  7. Loving the speed of this! I never thought text processing would ever be so fast in Flash.

    Now if only this (the spell checking part) was integrated in Snitter, it would save me and countless others from tweeting typo blunders.

  8. Bo – this is because of invalidation, which lets it run more efficiently. We can eliminate the delay, but it increases the potential CPU usage because it may redraw more than once per frame.

    Jerry – I’d need to know a bit more about what you’d like to do, but I can almost definitely say that portions of the code would be applicable, though it would likely require some extension.

Leave a Reply

Your email address will not be published. Required fields are marked *