Wednesday, January 7th, 2009
Typographize class that more or less does what it should.

Update: I noticed a few problems with some of the regular expressions governing em dashes. I've updated them accordingly, so if you're actually using this class, please re-download it!

Lots of stuff I do for work requires either closed captioning or walls of text—all of which gets loaded in through XML files.

Normally, I wouldn't bother with converting a normal quote (") to a "smart quote" (y'know, that crap Microsoft Word's always doing), but for some reason, I figured I'd give it a go. The following class is the result:

Typographize class (AS3)

I'm sure others have done something similar -- and quite possibly better -- but here's my attempt. It has a few methods that allow you to "smarten" either the entire blob of text or a subset of things (like double quotes, single quotes, em dashes, etc.). There's a method called only that may or may not work—I haven't tested it. At all. (But I do know its presence doesn't throw any errors, so good enough for now!)

It's poorly commented (when commented at all), but to use it, just plop it in your project's directory and try something like this:

var myString:String="This is an \"unsexified\" blob of 'text' --
I'm not even kidding! (You can also throw
<i class=\"my-italic-class\">\"a small subset of
Flash-supported HTML tags\"</i> in there, and it'll respect
the quotes [that is, it'll leave them alone] within the tag!)";
 
trace (Typographize.all(myString));

which will output the following:

This is an “unsexified” blob of text—I’m not even kidding! (You can also throw “a small subset of Flash-supported HTML tags” in there, and it’ll respect the quotes [that is, it’ll leave them alone] within the tag!

If you like it, well, there you go!

Note: For some reason, when tracing to the Output panel, the modified quotes (double and single) don't always show up; if you assign the output to the text or htmlText property of a textField, however, you'll see that everything does what it should.

Be the first to comment!

Leave a reply