I’m attempting to dynamically create a textField, and then assign one of three different styles to the text within it. In a sense I’m recreating what HTML does, by searching a text string for a few characters of text, then saves the location to an array. By stepping through the array I then use :
display.setTextFormat(resultsArM[counta],resultsArM[countb],titlev1);
to set the styles, inside a while loop.
Now the problem I’m having is removing the saught after string form the original text, and keeping the correct posistion values in the array.
Lets say I use:
content = “(m)Alex Cook(m) - (s)Designer(s)§I design y0§§(m)Thy Than(m) - Creative Director§She designs, y0”;
as the text… I want to search for (m) and assign titlev1 to the space between it, and continue to do the same for each ‘tag’ inside the text string, assigning a different style to the space between the tags.
Make sense?
The problem I’m having is that the array values are off by 6 the further into the string it searches… heres the current code which is a little cluddgy but I’m trying to nail the functionality down before I streamline it… help help help?
//SET UP SOME ARRAYS
charAr = new Array();
resultsArP = new Array();
//This searches for the (p) and replaces it with \r
charAr = content.split("(p)")
count = 0;
while (count<charAr.length) {
if (resultsArp.length <= 0) {
resultsArp.push(charAr[count].length)
} else {
numMod = resultsArp[count-1];
numMod = numMod + charAr[count].length;
resultsArp.push(numMod)
}
content = content.split("(p)").join("\r");
count++;
}
//Sets up a new charAr array, and creates resultsArM, which will hold the posistions of the (m) sequence in the string
charAr = new Array();
resultsArM = new Array();
charAr = content.split("(m)")
count = 0;
while (count<charAr.length) {
trace(resultsArM.length)
if (resultsArM.length <= 0) {
resultsArM.push(charAr[count].length)
} else {
numMod = resultsArM[count-1];
numMod = numMod + charAr[count].length;
resultsArM.push(numMod)
}
content = content.split("(m)").join("");
count++;
}
//trace(charAr);
trace(resultsArM);
//Sets up a new charAr array, and creates resultsArS, which will hold the posistions of the (s) sequence in the string
charAr = new Array();
resultsArS = new Array();
charAr = content.split("(s)")
count = 0;
while (count<charAr.length) {
trace(resultsArS.length)
if (resultsArS.length <= 0) {
resultsArS.push(charAr[count].length)
} else {
numMod = resultsArS[count-1];
numMod = numMod + charAr[count].length;
resultsArS.push(numMod)
}
content = content.split("(s)").join("");
count++;
}
//Set the text of the dynamic textField
display.text = content;
display.setTextFormat(textv1);
//Start moving through the arrays to assign styles to sections of the text
count = 0;
counta = 0;
countb = 1;
numMod = resultsArM.length;
numMod = numMod-1;
trace(numMod);
while (count<numMod) {
display.setTextFormat(resultsArM[counta],resultsArM[countb],titlev1);
counta = counta + 2;
countb = countb + 2;
count++;
}
count = 0;
counta = 0;
countb = 1;
numMod = resultsArS.length;
numMod = numMod-1;
trace(numMod);
while (count<numMod) {
display.setTextFormat(resultsArS[counta],resultsArS[countb],titlev2);
counta = counta + 2;
countb = countb + 2;
count++;
}
I dont know how to rectify this 6 characters off problem…
and to be honest, before I’m mocked for my code, I aint sure if .split is the way to go anyways, so suggestions will be much appreciated