Skip to main content
April 22, 2010
Answered

Color of String

  • April 22, 2010
  • 1 reply
  • 1389 views

Sorry, dont have any code at the moment because I saved it as CS4 and now it wont let me open the file at home on CS3 (or I think thats why it wont open!).

Anyway, will explain what I did.

I have a textfield where the user can enter text.  On the submit Button, the text field displays the text in a static text field.  Now, I wanted to also make each char of the inputted text change color.

So, I use the split method to split the text into a String Array.  for instance

var myArray:Array = myString.text.split();  //dont know if this is precisely right

Now, i set up a loop to loop through this array.  Now inside the loop, I wanted to change each char color.  So i done something like

myArray.toString().color = 0xFFFFFF;

but it says it cant apply a color to a String.  I have tried it without the toString but get a similar result.

So, how can I take text from a text field, split it into individual chars, and set the color of each char?

Any advise appreciated.

cheers

This topic has been closed for replies.
Correct answer kglad

What you have is pretty close. You could use an array of colors you cycle through, and then just create the text format inside your loop:

var cols:Array = new Array(0xFF0000, 0x00FF00, 0xFFFF00, 0x00FFFF, 0x0000FF);

function clickHandler(event:MouseEvent):void {
   for(var i : int = 0; i < myText.length; i++){
      
       var newFormat:TextFormat = new TextFormat();
       newFormat.color = cols[i % cols.length];
       myText.setTextFormat(newFormat, i);
   }
}

that should be:


var cols:Array = new Array(0xFF0000, 0x00FF00, 0xFFFF00, 0x00FFFF, 0x0000FF);

function clickHandler(event:MouseEvent):void {
   for(var i : int = 0; i < myText.text.length; i++){
      
       var newFormat:TextFormat = new TextFormat();
       newFormat.color = cols[i % cols.length];
       myText.setTextFormat(newFormat, i,i+1);  //and you should use that 3rd parameter (but opinions could vary)
   }
}

1 reply

kglad
Community Expert
Community Expert
April 22, 2010

you can use setTextFormat() to apply different textformat instances to each character.  textformat instances have a color property you can use to assign differnt colors.

April 22, 2010

So that means I dont even need to split the text into an array?

I could use

my_textField.setTextFormat(textFormat:TextFormat, beginIndex:int)

So i could easily change all the chars color to the same color using this, but thinking how to make each char a different color.

I am thinking this

var newFormat:TextFormat = new TextFormat();
newFormat.color = 0xFF0000;

function clickHandler(event:MouseEvent):void {
   for(var i : int = 0; i < myText.length; i++{
      
       myText.setTextFormat(newFormat, i);
   }
}

So that will loop every char, and apply the same color.  But what could i change to make them different colors?
What I create a new TextFormat for every color, and on every loop, do

myText.setTextFormat(newFormat+i, i);

that assumes my formats are name newFormat1, newFormat2 etc

Or is there a more logical way to do this?

cheers

April 22, 2010

What you have is pretty close. You could use an array of colors you cycle through, and then just create the text format inside your loop:

var cols:Array = new Array(0xFF0000, 0x00FF00, 0xFFFF00, 0x00FFFF, 0x0000FF);

function clickHandler(event:MouseEvent):void {
   for(var i : int = 0; i < myText.length; i++){
      
       var newFormat:TextFormat = new TextFormat();
       newFormat.color = cols[i % cols.length];
       myText.setTextFormat(newFormat, i);
   }
}