bgx:components logo
© 2004 -2005
Bernhard Gaul



Basic Flash Word Wrap Function

Below you find a function to insert linefeeds into a String with a certain character per line limit that can be used to achieve word wrapping in TextFields set to autoSize.

The function

function wrapText(txt:String,nChars:Number):String
{
	var paragraphs = txt.split(newline);
	var retTxt = "";
	for (var x = 0; x < paragraphs.length; x++)
	{
		var words = paragraphs[x].split(" ");
		var line = "";
	
		for (var i = 0; i < words.length; i++)
		{
			if ((line.length + words[i].length) > nChars)
			{
				retTxt += line + newline;
				line = "";
			}
			line += words[i] + " ";
		}
		retTxt += line;
		//add paragraph newline except for last paragraph
		if (x < paragraphs.length - 1) retTxt += newline;
	}
	return retTxt;
}
			
History

6-Jan-2005 : Added concept of paragraphs.