使用Actionscript 3在一个文本字段中的两种颜色

Kha*_*ara 11 actionscript-3

是否可以使用Actionscript 3.0在一个文本字段中使用两种文本颜色?

例如:我怎么能像第一个字符串黑色和第二个字符串红色?

使用单一颜色时,这是我的代码:

    public function logs(txt)
    {
        if (txt == '')
        {
            textLog.text = "Let's Open up our treasure boxes !!!";
        }
        else
        {
            textLog.text = '' + txt + '';
        }
        textLog.x = 38.60;
        textLog.y = 60.45;
        textLog.width = 354.50;
        textLog.height = 31.35;
        textLog.selectable = false;
        textLog.border = false;
        var format:TextFormat = new TextFormat();
        var myFont:Font = new Font1();
        format.color = 0x000000;
        format.font = myFont.fontName;
        format.size = 18;
        format.align = TextFormatAlign.CENTER;
        format.bold = false;
        textLog.embedFonts = true;
        textLog.setTextFormat(format);
        this.addChild(textLog);
    }
Run Code Online (Sandbox Code Playgroud)

Dio*_*ode 16

setTextFormat您可以指定起始索引和结束索引.您还可以使用html将文本渲染为html textLog.htmlText.

首先设置文本

var t:TextField  = new TextField();
t.text = "BLUEGREEN";
addChild(t);
Run Code Online (Sandbox Code Playgroud)

然后方法1

var format1:TextFormat = t.getTextFormat(0, 4);
format1.color = 0x0000ff;
t.setTextFormat(format1, 0, 4);


var format2:TextFormat = t.getTextFormat(5, t.length);
format2.color = 0x00ff00;
t.setTextFormat(format2, 5, t.length);
Run Code Online (Sandbox Code Playgroud)

或方法2

t.htmlText = '<font color="#0000ff">BLUE</font><font color="#00ff00">GREEN</font>';
Run Code Online (Sandbox Code Playgroud)