flash - word alignment in actionscript -


i have sentence string.

internal var sentence:string = "lorem ipsum dummy text of printing , typesetting industry."; 

then separated every word , stored in array named wordarray , apply text field , added stage using addchild.

for(var i:uint =0;x i< sentence.split(" ").length; i++){      wordarray.push(sentence.split(" ")[i]);      txt = new textfield();      addchild(txt);      txt.text = wordarray[i]; } 

alignement

if(i==0) { txt.x = wordarray[i].x;} else{ txt.x = wordarray[i -1].width + wordarray[i-1].x+2}; 

this align first line perfectly. want multiline particular width textfield.(if word exceeds boundary limit has come next line textfield)

?

firstly, i'll fix mistakes.

internal var sentence:string  var sentence:string 

the default access modifier internal. don't need write internal here. there no string class/object in as3. strings use string.


for (var i:uint = 0;x i< sentence.split(" ").length; i++){ 

what x before i <? remove it.


in code sentence splits multiple times. avoid split sentence once.

var splittedsentence:array = sentence.split(" ");  (var i: uint = 0; < splittedsentence.length; i++) 

next.

wordarray.push(sentence.split(" ")[i]); txt = new textfield(); addchild(txt); txt.text = wordarray[i]; 

you don't need store substring in wordarray. should store textfields.

txt = new textfield(); addchild(txt); txt.text = sentence.split(" ")[i]; wordarray.push(txt); 

define variable line stores current line of text (from 0).

var line:int = 0; 

set autosize property textfield:

txt.autosize = textfieldautosize.left; 

so width , height of textfield resized after text added textfield.


if (txt.x + txt.width > stage.stagewidth) {     txt.x = 0;     line++; } 

enter image description here

if current textfield out of bounds, moved next line.

finally, set y property of textfield:

txt.y = txt.height * line; 

full code:

import flash.text.textfield; import flash.text.textfieldautosize;  var sentence: string = "lorem ipsum dummy text of printing , typesetting industry."; var wordarray: array = []; var txt: textfield;  var line:int = 0;  var splittedsentence:array = sentence.split(" ");  (var i: uint = 0; < splittedsentence.length; i++) {     txt = new textfield();     txt.autosize = textfieldautosize.left;     addchild(txt);     txt.text = splittedsentence[i];      wordarray.push(txt);      if (i == 0)         txt.x = wordarray[i].x;     else         txt.x = wordarray[i - 1].width + wordarray[i - 1].x + 2;      if (txt.x + txt.width > stage.stagewidth)     {         txt.x = 0;         line++;     }      txt.y = txt.height * line; } 

the result:

enter image description here


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -