【问题标题】:Two colors in one text field using Actionscript 3使用 Actionscript 3 在一个文本字段中使用两种颜色
【发布时间】:2012-02-02 00:56:15
【问题描述】:

是否可以使用 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);
    }

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    setTextFormat 中可以指定开始索引和结束索引。您还可以使用 textLog.htmlText 将文本呈现为 html。

    先设置文字

    var t:TextField  = new TextField();
    t.text = "BLUEGREEN";
    addChild(t);
    

    然后方法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);
    

    或者方法2

    t.htmlText = '<font color="#0000ff">BLUE</font><font color="#00ff00">GREEN</font>';
    

    【讨论】:

    • 兄弟我该怎么做?你想举个例子吗?
    • 我已经用示例修改了答案。请检查。请记住,您需要在设置文本后应用格式。
    • 如果不想硬编码,请使用字符串的.length属性设置索引。
    • 感谢@Diode,但问题是文本是动态的,根据未定义的字符串,就像你发现的那样:等等……或者你找到了 100 个硬币,有时不是所有的文本是两种颜色,它只是一种颜色,无论如何它并非总是有两种颜色,而且并非所有颜色都具有相同的长度,你能解释一下我该怎么做这样的问题吗?,我已经编辑了这个问题,再次感谢二极管
    • 这就是为什么我要求使用字符串的.length 属性。在示例中,您必须使用 (0, firstString.length) 而不是 (0, 4)。
    【解决方案2】:

    如果你想这样做,你需要创建一个函数来控制。 charAt(在这里定义字符串的索引)。

        var format2:TextFormat = textbox.defaultTextFormat;
        format2.color = 0x000000;
        textbox.defaultTextFormat = format2;
    
        if((textbox.text.charAt(3) == "d") && ( textbox.text.charAt(4) == "i")){            
            var format1:TextFormat = textbox.defaultTextFormat;
            format1.color = 0xFF0000;
            textbox.setTextFormat(format1, 3, 5);}
        else{
            textbox.setTextFormat(textbox.defaultTextFormat);}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      相关资源
      最近更新 更多