Reputation: 290
I'm working on an exercise where children have to drag a textfield to the correct box. The textfield contains a word I loaded from an xml. When they drop the textfield in the correct box, I want to change the color of a part of the text in the textfield. This always has to be a specific letter / combination of letters.
For instance: they have to learn the difference between e and ee. If they drop a textfield with the word "ten" in the correct box, I want the "t" and the "n" to remain black, while the "e" needs to become blue. But if they drop the word "tween" in the correct box, the letters "ee" need to become blue.
I have been toying with setting the textFormat of a part of a string, but I seem to be unable to get this working. It's also hard because the number of characters of both options (e and ee) differ. But I am not getting anywhere.
tldr; I want to change the color of a part of the text in the textfield
Upvotes: 1
Views: 1294
Reputation: 13532
How about :
//setup a textformat
var textFormat:TextFormat = new TextFormat();
var startIndex:int = someString.indexOf("e");
textField.setTextFormat(textFormat,startIndex,startIndex+1);
Upvotes: 2
Reputation: 290
I've come to the conclusion the best way to change the colour of a part of a word is to use the Stylesheet class. I took the snippet below from someone here on Stackoverflow but I forgot to note who it was. I'm sorry, whoever you are.
var style:StyleSheet = new StyleSheet();
var styleObj:Object = new Object();
styleObj.fontSize = "bold";
styleObj.color = "#FF0000";
style.setStyle(".red",styleObj);
var tf:TextField = new TextField();
tf.styleSheet = style;
tf.htmlText = "fox is <span class='red'>red</span>";
addChild(tf);
Upvotes: 0
Reputation: 5577
To change the color of some characters in the middle of a string use html: http://cartoonsmartblog.wordpress.com/2009/08/28/coloring-html-text-in-flash-actionscript-3-quick-code-note/
(btw just use single quotes and double quotes instead of that \u0022 ugliness)
Upvotes: 0