May 7th, 2009
- Flash
Orphans and Widows
If I've only learned one thing from designers, its that they hate orphans/widows. As a flash developer I often have to work with external text and there is a good chance you'll end up with an orphan.
But I was playing with a few new AS3 TextField methods and was able to come up with a nice function to help minimize orphans. This is far from perfect, but it should at least provide a good starting point for a more robust function.
function fixOrphan( tf:TextField ):void {
if( tf.numLines > 1 ) {
var s:String = tf.getLineText( tf.numLines-1 );
// Orphan is present, let's adopt this sucker
if( s.indexOf(" ") < 0 ) {
var tempString:String = tf.text.substring( 0, tf.text.lastIndexOf(" ") );
var firstPart:String = tempString.substring( 0, tempString.lastIndexOf(" ") );
var lastPart:String = tf.text.substring( tempString.lastIndexOf(" ")+1 )
tf.text = firstPart + "\n" + lastPart;
}
}
}