as3 html文本,as3.0中为TextField的文字添加样式

为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值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AS3.0 TLFTextField 最新的组件库 以往的有BUG,这个可是最新的哦 package { import fl.controls.UIScrollBar; import fl.text.TLFTextField; import flash.events.Event; import flashx.textLayout.conversion.TextConverter; import flashx.textLayout.edit.IEditManager; import flashx.textLayout.elements.FlowElement; import flashx.textLayout.elements.InlineGraphicElement; import flashx.textLayout.elements.ParagraphElement; import flashx.textLayout.elements.TextFlow; import flashx.textLayout.events.CompositionCompleteEvent; import flashx.textLayout.events.StatusChangeEvent; /** * 使用 TLFTextField 类创建使用文本布局框架 (TLF) 的高级文本显示功能的文本字段。 * TLFTextField 对象与 TextField 对象类似,但 TLFTextField 对象可以利用 flashx 包包含的 TLF 类的属性和方法。TLF 提供了大量格式选项和功能。 * @author */ public class STLFTextField extends TLFTextField { //最大段落数 private var _maxParagraph:uint = 0; //UI滚动条 private var uiScrollBar:UIScrollBar; public function STLFTextField() { super(); } private function tlfEventHandle(e:Event):void { super.textFlow.flowComposer.updateAllControllers(); if (uiScrollBar) { uiScrollBar.scrollPosition = uiScrollBar.maxScrollPosition; uiScrollBar.validateNow(); } } /** * 过滤段落数达到最大值时文本的段落 */ private function filtrationPar():void { if (_maxParagraph == 0) return; if (textFlow.numChildren > _maxParagraph) { for (var i:uint = 0; i < textFlow.numChildren - _maxParagraph; i++ ) { textFlow.removeChildAt(0); } } } /** * 在文本插入图片 * @param src 图片的路径或者库里面的MC实例 * @param width 图片的宽,默认是图片宽度 * @param height 图片的高,默认是图片的高度 */ public function insertImg(src:Object, width:Object = "auto", height:Object = "auto"):void { if(!textFlow.hasEventListener(StatusChangeEvent.INLINE_GRAPHIC_STATUS_CHANGE)) super.textFlow.addEventListener(StatusChangeEvent.INLINE_GRAPHIC_STATUS_CHANGE, tlfEventHandle); if(super.textFlow.interactionManager is IEditManager) { IEditManager(super.textFlow.interactionManager).insertInlineGraphic(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值