[AS3]as3处理文本,处理文本内容,格式化文本框文本
有三种方式对文本进行格式化:HTML标签格式化、使用TextFormat对象、CSS样式
* HTML标签格式化示例: textBox.htmlText = "<B>粗体文本</B><U>下划线文本</U>";
* TextFormat对象示例:(注意:只对setTextFormat之前的代码有效)
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFF0000;
textFormat.htmlText = "有效";
//也可以对部分文字进行格式化,例如:textBox.setTextFormat(textFormat,3,textBox.text.length);
textBox.setTextFormat(textFormat); textFormat.htmlText = "无效";
* CSS样式示例:(注意:只对CSS定义之后的代码有效)
var css:StyleSheet = new StyleSheet();
var styleObj:Object = {color:"#FF0000"};
css.setStyle(".stdStyle",styleObj);
textBox.styleSheet = css;
textBox.htmlText = "不潮不用花钱";
this.addChild(textBox);
- package {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.text.StyleSheet;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- public class Sample0324 extends Sprite
- {
- private var label:TextField;
- private var text:String = "here comes <span class='stdText'>wayne</span>";
- public function Sample0324()
- {
- var btnRed:TextField = new TextField();
- btnRed.text = "CuPlayer.com提示:应用红色样式";
- btnRed.y = 100;
- btnRed.addEventListener(MouseEvent.CLICK,onRedClick);
- this.addChild(btnRed);
- var btnGreen:TextField = new TextField();
- btnGreen.text = "CuPlayer.com提示:应用绿色样式";
- btnGreen.y = 200;
- btnGreen.addEventListener(MouseEvent.CLICK,onGreenClick);
- this.addChild(btnGreen);
- label = new TextField();
- label.autoSize = TextFieldAutoSize.CENTER;
- label.htmlText = text;
- this.addChild(label);
- }
- private function onRedClick(event:MouseEvent):void
- {
- var loader:URLLoader = new URLLoader();
- loader.load(new URLRequest("StdStyle.css"));
- loader.addEventListener(Event.COMPLETE,onLoadComplete);
- }
- private function onGreenClick(event:MouseEvent):void
- {
- var loader:URLLoader = new URLLoader();
- loader.load(new URLRequest("StdStyle2.css"));
- loader.addEventListener(Event.COMPLETE,onLoadComplete);
- }
- private function onLoadComplete(event:Event):void
- {
- var css:StyleSheet = new StyleSheet();
- css.parseCSS((event.target as URLLoader).data);
- label.styleSheet = css;
- label.htmlText = text;
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例