为了在单个文本字段上实现不同的颜色,您必须在文本字段上使用此属性:
myTextField.html = true
myTextField.htmlText = 'bli bli bli<font color="#0000FF">bla bla bla/font>'
或者您可以使用 TextFormat 类来执行此操作。
这是你可以做的。
tField.text = "bli bli bli";
var tFormat:TextFormat = new TextFormat();
tFormat.color = 0xff0000;
tField.setTextFormat(0,5,tFormat);
tFormat.color = 0x33cc33;
tField.setTextFormat(5,9,tFormat);
为了在您键入时获得颜色,请使用此类:
package {
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class ColorTextField extends TextField{
var tf:TextFormat = new TextFormat();
var ar:Array = new Array(0xFF0000,0x00FF00,0x0000FF,0x123456);
public function ColorTextField() {
// constructor code
this.type = TextFieldType.INPUT;
tf.size = 33;
this.defaultTextFormat = tf;
this.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
}
private function onKeyUp(event:KeyboardEvent):void{
var index:int = 0;
var colorIndex:int = 0;
while (index < this.text.length){
var char:String = this.text.substr(index,1);
if(char == " "){
colorIndex = 0;
}else{
tf.color = ar[colorIndex];
trace(index + "-" +ar[colorIndex]);
this.setTextFormat(tf,index,index+1);
colorIndex++;
if(colorIndex > ar.length-1){
colorIndex = ar.length-1;
}
}
index++;
}
}
}
}
这就是你实现它的方式。创建一个新的 AS3 Fla 并将其分配为基类:
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
public class MyClass extends MovieClip {
var tf:ColorTextField = new ColorTextField();
public function MyClass() {
// constructor code
tf.width = 500;
tf.text = "12345";
this.addChild(tf);
}
}
}
输入新文本的位置无关紧要