2012-01-04 20 views
11

¿Es posible tener dos colores de texto en un campo de texto usando Actionscript 3.0?Dos colores en un campo de texto usando Actionscript 3

ex: ¿cómo puedo hacer que la primera cadena sea negra y la segunda cadena roja?

Aquí está mi código cuando se utiliza un solo color:

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); 
    } 

Respuesta

16

En setTextFormat puede especificar índice inicial y el índice final. También puede renderizar texto como html usando textLog.htmlText.

En primer lugar establece el texto

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

Entonces método 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); 

o método 2

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

¿cómo puedo hacer ese hermano? te gustaría dar un ejemplo? –

+0

He modificado la respuesta con una muestra. Por favor, compruebe. Recuerde que debe aplicar el formato después de configurar el texto. – Diode

+0

Utilice '.length' propiedad de cadena para establecer índices si no desea el código difícil. – Diode

0

Si usted quiere hacer de esa manera, es necesario crear una función para el control . charAt (DEFINA EL ÍNDICE DE STRING AQUÍ).

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);} 
Cuestiones relacionadas