TextField autoSize drama
Date: 22 February 2010About 50% of all my flash projects I used dynamic textfields that resizes according to it's content.
And ofcourse my blog would not be here if textfields were unable to auto resize. Unfortunately the autoSize property is not working well in some cases. First of all take a small peak at the autoSize documentation here.
The problem most of the time occurs on multiline textfields.
Some glitches of the autoSize property
- TextField height is not accurate.- TextField allows vertical scrolling.
- No trimming
So I started playing around with the textHeight, mouseWheel and scroll event and left the autoSize as it is and I came up some interesting stuff.
The above example uses the following trimming function:
/** Trim multiline string
* @usage var string:String = trim("Lorem\nipsum\n");
**/
function trim($str:String):String {
var lastLineReached:Boolean = false;
var newLines:Array = [];
var substractLines = 0;
for (var i:int = txt4.numLines-1; !lastLineReached; i--) {
if (txt4.getLineText(i).length > 0) {
lastLineReached = true;
} else {
substractLines ++;
}
}
for (i = 0; i < txt4.numLines - substractLines; i++) {
newLines.push(txt4.getLineText(i).replace(/(\n|\r)/g,""));
}
return newLines.join("\n");
}
Comments