Change color of text in ActionScript3 after exact character

53 Views Asked by At

I want to know if it is possible to change font color in dynamic text field after exact character, for example I want text after : to be blue.

2

There are 2 best solutions below

0
On

You can count where in the string is exact character and use length of splited string on this character:

var my_str:String = "This text is black: and this text is white";
var my_array:Array = my_str.split(":");

var testText:TextField  = new TextField();
testText.text = my_str;
addChild(testText);

var format1:TextFormat = testText.getTextFormat(0, my_array[0].length);
format1.color = 0x000000;
testText.setTextFormat(format1, 0, my_array[0].length);


var format2:TextFormat = testText.getTextFormat(my_array[0].length+1, testText.length);
format2.color = 0xffffff;
testText.setTextFormat(format2, my_array[0].length+1, testText.length);
0
On