How can i change color of one word in a dynamic textField in AS 3
-
30-06-2021 - |
Pergunta
I have this function:
private function boldVerb(_phrase:String, _verb:String):String
{
var newHtmlText:String = "";
var pattern:RegExp = new RegExp([_verb]);
newHtmlText = _phrase.replace(pattern, "<b>" + _verb + "</b>");
return newHtmlText;
}
And I want to change the color of this "verb" that the function receives. Is it possible in AS3?
Solução
Sure. You can either use a stylesheet or you use the font
tag (assuming it's an html textfield).
<font color='#FF0000'>This is red</font>
See documentation here.
Outras dicas
Also, as this is the HTML formatted text you could have a stylesheet with .verb class and then tag span with class attribute verb will paint it however you want - this is for greater flexibility.
private function boldVerb(_phrase:String, _verb:String):String
{
var newHtmlText:String = "";
var format:TextFormat = new TextFormat();
format.color = 0x990000;
var pattern:RegExp = new RegExp([_verb]);
newHtmlText = _phrase.replace(pattern, "<b>" + _verb + "</b>");
textField.setTextFormat(format);
return newHtmlText;
}
If textField is a text field on the stage this should work, but this is how you do text formatting in AS3, do a google search on "AS3 TextFormat" for more info. First, create a new TextFormat object, and give it a property, I chose a dark red #990000 (0x990000 in AS) and then applied it to the text field. I am sure you're wanting to format the string itself, which I don't recall if that's possible in Flash, but you can certainly edit the text field itself. So you may have to string together some text fields to get a string of text with one accented word. hopefully that gets you a step closer! Good luck!