FLASH AS3 TextField

一、TextFormat必须要设置在textField.text赋值之后。否则TextFormat样式调用不进去。尤其是对中文如此。但实际应用中,常先设置文本类,具体调用时再赋值。这样,TextFormat的作用就失去了。所以,强调先给text赋值,再设置TextFormat。
二、原以为textField属于InteractiveObjec类,即可交互,可直接触发TextEvent监听事件,实则不然,加监听前需设置XML 的href="event:myText"。我的理解:文本不能出发event,需要单独写名“event:”事件,另外,textfield需要.htmlText形式赋值。设置好textfield的内容赋值形式及XML,即可加监听了。
程序代码 程序代码
//htmlText方法:
var tmp1_text:TextField=new TextField();
tmp1_text.x=10;
tmp1_text.y=10;
tmp1_text.width=200;
tmp1_text.height=60;
tmp1_text.htmlText = "<font color='#0033FF'>蓝色</font><font color='#ff0000'>红色</font><font color='#FF6600'>橙色</font>" +"\n" +
                       "<font color='#666666' size='11'>灰色</font><font color='#000000'>黑色</font><b><font color='#33CC00' size='18'>绿色</font></b>" +"\n";
addChild(tmp1_text);

//----------------------------------------------------------------------------------
//TextFormat方法:
var tmp2_text:TextField=new TextField();
var format1:TextFormat=new TextFormat();
var format2:TextFormat=new TextFormat();

tmp2_text.x=10;
tmp2_text.y=60;
tmp2_text.width=300;
tmp2_text.height=30;
tmp2_text.text="Case Study: The E8 Particle Simulator for Garrett...";

format1.size=15;
format2.size=25;
format1.color=0xff0000;
format2.color=0x00ff00;

tmp2_text.setTextFormat(format1,3,7);
tmp2_text.setTextFormat(format2,8,11);

addChild(tmp2_text);

//----------------------------------------------------------------------------------
//htmlText+css方法:
var tmp3_text:TextField=new TextField();
var style:StyleSheet = new StyleSheet();

tmp3_text.x=10;
tmp3_text.y=100;
tmp3_text.width=300;
tmp3_text.height=20;

style.setStyle(".style1",{color:'#000000',fontWeight:'bold'});
style.setStyle(".style2",{color:'#ff0000',fontSize:'15'});
style.setStyle("a:link", { color: '#006600', textDecoration: 'underline',fontSize:'12' });
style.setStyle("a:hover",{color:'#003399',fontSize:'12'});
style.setStyle("a:visited",{color:'#990000',fontSize:'12'});
style.setStyle("a:active ",{color:'#000000',fontSize:'12'});

tmp3_text.styleSheet = style;
tmp3_text.htmlText="<a href='#'>超链接</a>sdfwewe<span class='style1'>sdlfjsiodf fwekfw</span><span class='style2'> 看吧看吧</span>";

addChild(tmp3_text);

  



Htmltext中加入事件侦听,替换内容:

程序代码 程序代码
var tmp_txt:TextField=new TextField();
addChild(tmp_txt);
tmp_txt.htmlText="<a href='event:th'>替换</a>";
tmp_txt.addEventListener(MouseEvent.CLICK,txtHandler);
function txtHandler(evt:MouseEvent):void {
    var xtxt:String=(tmp_txt.htmlText).split("替换").join("非常棒!");
    tmp_txt.htmlText=xtxt;
}

  



mp3列表曲目侦听,并应用样式:

程序代码 程序代码
var style:StyleSheet = new StyleSheet();
style.setStyle("a:link", { color: '#006600', textDecoration: 'underline',fontSize:'12' });
style.setStyle("a:hover",{color:'#003399',fontSize:'12'});
style.setStyle("a:visited",{color:'#990000',fontSize:'12'});
style.setStyle("a:active ",{color:'#000000',fontSize:'12'});

var list:TextField = new TextField();
list.autoSize = TextFieldAutoSize.LEFT;
list.multiline = true;
list.styleSheet = style;
list.htmlText = "<a href="event:track1.mp3">Track 1</a><br>";
list.htmlText += "<a href="event:track2.mp3">Track 2</a><br>";
addEventListener(TextEvent.LINK, linkHandler);
addChild(list);

function linkHandler(linkEvent:TextEvent):void {
        trace(linkEvent.text);
}

  


<img>标签鼠标单击侦听:

 
var txt:TextField=new TextField();
txt.width=300;
txt.;
txt.htmlText = '<div id="blogMar">'+
' <img  id="icon" src="http://img1.qq.com/blog/pics/15256/15256935.jpg" alt="" /><br>'+
'999999999999999999999999999999999999999</div>';
addChild(txt);

var emb :Loader= txt.getImageReference("icon") as Loader;
emb.addEventListener(MouseEvent.CLICK, onTextClick);

function onTextClick(e:MouseEvent):void {
    trace("click");
    //launch URL or whatever
}

  

原文地址:https://www.cnblogs.com/tankaixiong/p/2933507.html