This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. This example dynamically reflows text based on user input. It is used to replace certain spaces with line breaks in existing text but it could be modified to find and replace entire strings.
Before you read any further I want to make it clear that using html-enabled textboxes causes this function to create unexpected line breaks because there is no way for the current reflow() function to distinguish between html and display text. Using html-enabled textboxes WILL increase the minimum pixels per line length (which renders the function pretty much useless). If you attempt to rework it to account for HTML feel free to contact me with questions about the existing code or to share the reworked code.
Font Choice Note:
The value used to calculate the charPerLine variable (.24 or 24 letters per 100 pixels) is determined by the typeface you choose. For this example I used a face specially developed for use with Flash, standard 07_53, designed by Craig Kroeger and is available for free at www.miniml.com (be sure to read the READ_ME). If you wish to use another font look in the movie clip "charactersPer100px" for the necessary tools to calculate this value.
Please download the source FLA to view the document set up. It's ridiculously simple - only four symbols (and two aren't even necessary for the script to work) - so I'll jump straight into the code that is all on frame 1 of the main movie:
[as]
var initText = "...(your text here)";[/as]
[as]var charPer100px = .24;[/as]
This sets the number of characters tat can fit in a width of one hundred pixels. If you're not using the font "standard 07_53" this number may be different.[as]function reflow () {
if (lineLength && lineLength<530 && lineLength>99) {[/as]
[as] // trace ("i hear you - i just don't care");[/as]
If for some reason you have moved the button that calls reflow() into another movie and it no longer works uncomment this trace() to make sure that you are targeting it correctly.[as] var charPerLine = Math.floor(Number(lineLength)*Number(charPer100px));[/as]
This determines the number of letters per line for the entered lineLength[as] var initLinesReq = Math.ceil(initText.length/charPerLine);[/as]
This is an initial estimation of the number of lines the new text will require. This value will be updated throughout the reflowing function.[as] var linesReq = initLinesReq;[/as]
The number of times the for() will loop.[as] var reflow;[/as]
An empty variable that the for() will fill with our reflowed text.[as] for (var i = 0; i <= Number(linesReq); i++) {
var idealBreak = Number(prevBreak + charPerLine);[/as]
[as] if (i != Number(linesReq)) {[/as]
"If this isn't the last time through the loop..." This is a necessary distinction. Without it the last word would always be isolated on its own line.[as] var nextLine = (initText.substring(Number(prevBreak),
Number(initText.lastIndexOf(" ", idealBreak))));[/as]
[as] var prevBreak = Number(initText.lastIndexOf(" ", idealBreak)+1);[/as]
"Remembers" the index of the character after the space that nextLine "copied" up to. For use when determining the idealBreak in the next loop.[as] var remainText = (initText.substring(
Number(initText.lastIndexOf(" ", idealBreak)+1)));[/as]
[as] freshLinesReq = Math.ceil(remainText.length/charPerLine);[/as]
Updates the number of remaining loops.[as] linesReq = freshLinesReq;
i=0;[/as]
[as] reflow += nextLine+" ";[/as]
"Pastes" nextLine to the existing reflowed text[as] }
else {[/as]
[as] nextLine = (initText.substring(prevBreak));[/as]
"Copy" the remaining a text.[as] reflow += nextLine;[/as]
"Pastes" the final bit of text into reflow.[as] }
}
text=reflow;[/as]
[as] }
else {
text="please enter a number less than 530 (the width of this swf)
and greater than 99.";[/as]
[as] }
}
[/as]