
Page 1 of 1
Shaun Inman
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.
View all articles by Shaun InmanWritten by: Shaun Inman
Time: 30 min. tops
Difficulty Level: Advanced
Requirements: Flash 5 (the pixel font "standard 07_53" is recommended)
Assumed Knowledge: user-defined functions, basic understanding of the use of array and string indexes
Download the source.
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:
var initText = "...(your text here)";
var charPer100px = .24;
function reflow () {
if (lineLength && lineLength<530 && lineLength>99) {
// trace ("i hear you - i just don't care");
var charPerLine = Math.floor(Number(lineLength)*Number(charPer100px));
var initLinesReq = Math.ceil(initText.length/charPerLine);
var linesReq = initLinesReq;
var reflow;
for (var i = 0; i <= Number(linesReq); i++) {
var idealBreak = Number(prevBreak + charPerLine);
if (i != Number(linesReq)) {
var nextLine = (initText.substring(Number(prevBreak),
Number(initText.lastIndexOf(" ", idealBreak))));
var prevBreak = Number(initText.lastIndexOf(" ", idealBreak)+1);
var remainText = (initText.substring(
Number(initText.lastIndexOf(" ", idealBreak)+1)));
freshLinesReq = Math.ceil(remainText.length/charPerLine);
linesReq = freshLinesReq;
i=0;
reflow += nextLine+" ";
}
else {
nextLine = (initText.substring(prevBreak));
reflow += nextLine;
}
}
text=reflow;
}
else {
text="please enter a number less than 530 (the width of this swf)
and greater than 99.";
}
}

