Hyphenation in ActionScript

There are a lot of nice text features in the new flash text layout framework but obviosly there is still one important feature missing: Hyphenation. Until today, there is no native hyphenation support in the flashplayer. I decided to fill this gap by porting the Hyphenator.js library by Mathias Nater to ActionScript. In this post I’ll demonstrate text hyphenation in flash.

Demonstration

Here you see the hyphenation in action. You can choose one of the currently supported languages and resize the text field to see the hyphenation changes.

Usage

The usage is pretty simple. To hyphenate the text of a textfield you only have to call the hyphenateTextField() function and provide the text language and the textfield instance as parameters.

// creating a new textfield first
var tf:TextField = new TextField();
tf.multiline = true;
tf.width = 400;
tf.text = 'long text that goes over multiple lines';
addChild(tf);
// let's hyphenate
Hyphenator.hyphenateTextField('en', tf);

Download & License

You can download as3-hyphenation from the corresponding bitbucket repository page or via the direct zip link . It is published under the LGPL.


Update (May 20, 2010): the hereby presented algorithm does only work for textfields without stylesheets. I’ll try to extend it in the next weeks.

Update: I extended the Hyphenation class with support for html text and stylesheets. Thanks to Gary McGhee an his port of John Resig’s HtmlParser.