Search Box

Google
 

Thursday, July 05, 2007

Being Greedy for finding WordWrap in Flash.

Hello All,

Again after a longtime here i come to talk about learning a way to find the breaking position in a Dynamic/Input Textbox. Wait did i mean "Finding Wordwrap positions in a Dynamic Textbox " ?. Yes you thought right. I am here to discuss an algorithm to find the positions wherever Wordwrap happens to appear in a Dynamic Textbox in flash. But beware it is very greedy and quite possible that it might eat your processor. Still check out wikipedia on wordwrap and you will find "greedy" type algorithm to be one of the ways to find wordwrap positions in general.

Okay let me not take this post further by posting code which again will not ensure that all of us might understand it.

But before you proceed brush yourself about the TextFormat class and especially the "getTextExtent" method. I hope you would have guessed what i am trying to do. Yeah, Inorder to find the breaking positions within a TextBox this method is sufficient enough to find the answer/solution.

Wait does flash not have a method to do this ? i would say a BIG NO. For that instance not only flash but any application would not have a method to find the wordwrap position. Do you know the reason why ? Since the wordwrapping is handled by the applications UI Algorithm and does not insert nor append any line formatting characters such as "\n" or "\t" etc., at the breaking point so that we can break the text using the split method using these characters.

Instead the solution that worked for me was getting the current TextFormat of the textbox we need to find wordwrap positions using getTextFormat(). Using this textformat we take the string content of the textbox and then using "WHILE" loop i move the character index one by one and store it in a temporary string "tempString" thereby also checking simultaneously the width in pixels which i get by applying MyTextFormat.getTextExtent(tempString) to see if the textWidth returned has not crossed more the width of the textbox for which we are currently checking.

Once if i find the condition of tempString width being more than the currentTextBox Width is successfully satisfied, i remove that part of the tempString from the original string using :

originalString = originalString.substring(tempString.length-1,originalString.length);
someArray.push(tempString.substring(0,tempString.length-1));
// REmember that the length has exeeded so the breaking point is the length -1 ;

//Now clear the tempString

tempString = '';


This process is carried until after all the characters are removed from my original string meaning (originalString == ''").

After this algorithm has finished running i would end up with an array containing strings. From the array length once can find the total number of lines and also the way the lines have been split up in a textbox with any font, size etc.,

Hope you understood the intention and its solution posted in this blog.

Regards,

Ashok Srinivasan.