本文主要是介绍AS3聊天表情即图文混排源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
var obj:GIFPlayer = new GIFPlayer();是引用的外部类库,可以自己下载,我的资源中也有,欢迎下载:package controller.divChat
{import config.ConfigVar;import flash.display.Sprite;import flash.events.Event;import flash.net.URLRequest;import flash.text.StyleSheet;import flash.text.TextField;import org.bytearray.gif.player.GIFPlayer;public class CH_ReplaceFace{public function CH_ReplaceFace(){}//打印聊天文本段public static function sendStaticFont(text:TextField,sp:Sprite,htmlFontStr:String,overFun:Function=null):int{var backH:int;text.htmlText = htmlFontStr;text.width = 190;text.multiline = true;text.wordWrap = true;text.autoSize = "left";sp.addChild(text);var style:StyleSheet = new StyleSheet();style.setStyle("a:hover",{color:'#FF0000',fontSize:'14'});text.styleSheet = style;checkFaces(text,overFun);//trace(sp.height+" ,, "+text.height);backH = text.height;return backH;}//添加表情public static function checkFaces(tf:TextField,overFun:Function=null):void{var htmlStr:String;var textStr:String;var indexAry:Array = new Array();//表情索引数组var pointObj:Array = new Array();//表情占位坐标数组var faceSign:Array = new Array();//表情符数组htmlStr = tf.htmlText;//表情在聊天内容的符号为: /:02 , 样式说明:/:为两个字符的标识符,02表示表情数;可以自己换成想要的。var reg:RegExp = /\[:\d+:\]/g;faceSign = htmlStr.match(reg);//这里只搜索到两个表情符号if (faceSign.length == 0)return;//下面又把表情符号替换为空,接着下面去搜索空。如果下面去搜索表情符号tf.htmlText = htmlStr.replace(reg, "<font size=\'24\'> </font>");//把文本框内的内容用<font>字体标签代替字符串htmlStr中的表情符号var ii:uint = 0;textStr = tf.text;//再把文本框内的文本,非html文本赋给另一个字符串对象textStrwhile (1){indexAry.push(textStr.indexOf(" ", ii));//循环搜索textField.text中的空格(即上面利用<font>标签替换的字符)索引位置,保存索引位置到indexAry数组中if (indexAry[indexAry.length-1] == -1){//| |indexAry.pop();break;}ii = indexAry[indexAry.length-1] + 1;}var jj:uint = 0;var storeHeight:Number=tf.height;//把textField行高赋给临时变量while (jj< indexAry.length){tf.height=tf.textHeight;//把textField文本高赋给textField行高pointObj.push(tf.getCharBoundaries(indexAry[jj]));//保存表情坐标位置到数组tf.height=storeHeight;//把临时变量再赋给行高,还原。jj++;}faceSign = faceSign.reverse();//反转表情符号数组pointObj = pointObj.reverse();//反转坐标对象数组var kk:uint = 0;while (kk< faceSign.length){if (pointObj[kk] != null){//创建一个表情对象var str1:String = faceSign[kk].toString().replace("[:","");var str2:String = str1.replace(":]","");var n:uint = parseInt(str2);var obj:GIFPlayer = new GIFPlayer();obj.load ( new URLRequest(ConfigVar.facePath+"face1_"+n+".gif"));tf.parent.addChild(obj);obj.name = "faces";//obj.gotoAndStop(n);//由于表情是以一个MC多帧来保存,所以计算出来的表情数,直接跳帧显示obj.x = pointObj[kk].x;//设置表情的x坐标obj.y = pointObj[kk].y;//设置表情的y坐标obj = null;kk++;}}reg = null;faceSign = null;pointObj = null;indexAry = null;textStr = null;htmlStr = null;if(overFun!=null){overFun();}}}
}
这篇关于AS3聊天表情即图文混排源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!