为TextField中文字添加样式的方式有多种,下面只说我常用的一种。通过TextField的htmlText属性及styleSheet属性来为文字添加样式。htmlText可以为文字添加标签,而styleSheet则为标签中添加的class定义样式。自摘代码片段如下:public class Main extends Sprite
{
private var userNameInfo:TextField;
public function Main()
{
this.init();
this.doDrawInit();
}
private function init():void {
this.userNameInfo = new TextField();
this.userNameInfo.width = 300;
this.userNameInfo.type = TextFieldType.INPUT;
this.userNameInfo.background = true;
this.userNameInfo.backgroundColor = 0xeeeeee;
this.userNameInfo.x = 10;
this.userNameInfo.y = 10;
var newStyle:StyleSheet = new StyleSheet();//(1)
var styleObj:Object = new Object();
styleObj.fontWeight = "bold";
styleObj.color = "#660066";
styleObj.fontSize = 30;
styleObj.fontStyle = "normal"; //可识别的值为 normal 和 italic。
styleObj.display = "block";//受支持的值为 inline、block 和 none。
styleObj.textAlign = "center";//可识别的值为 left、center、right 和 justify。
newStyle.setStyle(".defStyle", styleObj);
this.userNameInfo.styleSheet=newStyle;
this.userNameInfo.htmlText = '用户名';
this.doDrawInit();
var textinfo :TextLineMetrics = this.userNameInfo.getLineMetrics(0);
}
private function doDrawInit():void {
this.addChild(this.userNameInfo);
}
}
可以分析上面的代码可得,首先定义StyleSheet(1),然后为利用该对象中的setStyle方法,定义一个class=deFStyle的样式对象,再一步把需要设定该样式的TextField对象进行属性设定,当然设定其styleSheet属性为之前定义的StyleSheet对象。最以用TextField的htmlText来设定文本,当然同时带有标签,只是标签中添加一个class属性,其值为之前设定的class值。