flash与字符串:替换表情(转载 夏天的树人的专栏)

    技术2025-10-20  11

    http://blog.csdn.net/hero82748274/archive/2011/01/13/6136847.aspx

     

    今天想记下一些笔记,先感谢一下六指小龙龙的帮忙。其实这个是他写的,我在基础上修改一点,顺便记录一下相关的记录。在制作微博客户端时候,可以加入这个功能去。制作要求是替换中括号里面的内容,让他变成相关的图片链接。

    例如:[ 哈哈 ] 对应链接是 <img src="1.gif"/>

             [ 呵呵] 对应链接是 <img src="2.gif"/>

             [ 嘻嘻 ] 对应链接是 <img src="3.gif"/>

    替换出来的变成html的形式了,这样可以在flash 10 里面利用新的文本引擎制作相关的事情了。至于9.0 其实很多人都已经实现了,这个网上很多源码,而且解决方法很多很实用。我们只是理解一下思路就行。至于办法多种多样,我们去记录一下。

     

    例如一段微博当中内容:当中会返回一些带有表情的字符串,如[哈哈] [太开心] [嘻嘻] 这样的表情,那么采用正则去替换相应的事情就是一件有趣的事情,因为正则强大可以解决这些字符串相应替换的问题,但是正则相对来讲比较难看,要经常写才能有感觉,个人感觉有时候吃不消。

    现在测试一段文字:

     如: 这是一个大坏蛋,[哈哈],不过他对我好好的[嘻嘻] [呵呵] ,你说是不是呢OMG[888]?

     

    测试环境在flash cs5的情况下进行,采用两种文本进行对比。一种是传统的TextField 另外一种是TLF,对比出来的结果是不相同。

    采用方式差别就可以在图中看出来,但是相对来讲TLF 不是适应很多场景,adobe官方文档里面有提及到这种新文本引擎,弊端的之处。所以使用的时候根据开发而定,而目前来讲,对付这个大怪物确实有点难消耗。毕竟TLF设计有点让人难以接受。比起TextField的简洁我会倾向这种设计。

    注意:TLFText 是在舞台创建的TFL文本。

     

    可以以下面的测试进行测试:

    A:这是一个大坏蛋不过他对我好好的你说是不是呢OMG?(不含表情)

    B:"这是一个大坏蛋,[HH],[哈哈]不过他对我好好的[笨蛋] [OK] ,[2dk]你说是不是呢OMG[888]?";(含有表情)

    C:这是一个大坏蛋,[HH],不过他对我好好的[笨蛋] [OK] ,[2dk]你说是不是呢OMG[888]?";(含有中括号 不含有对应的表情)

    测试代码:

    view plaincopy to clipboardprint?import flash.text.TextField;     var str:String = "这是一个大坏蛋,[哈哈],不过他对我好好的[嘻嘻] [呵呵] ,你说是不是呢OMG[888]?";     var msg:TextField=new TextField();   msg.autoSize = TextFieldAutoSize.LEFT;   msg.multiline = true;//允许多行,要<br>发挥作用就要设置   msg.wordWrap = true;//自动换行,要图片能看就必须要设置   msg.width = 200;    msg.height = 200;   msg.border=true;    addChild(msg);   var myString:Array=new Array();   myString.push("[呵呵]","[嘻嘻]","[哈哈]","[爱你]","[晕]","[泪]","[馋嘴]","[抓狂]","[哼]","[可爱]");     //表情地址   var rep:Array=new Array();   rep.push("<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/eb/smile.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/eb/smile.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/c2/tooth.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/c2/tooth.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/6a/laugh.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/6a/laugh.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/7e/love.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/7e/love.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/a4/dizzy.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/a4/dizzy.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/d8/sad.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/d8/sad.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/b8/cz_org.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/b8/cz_org.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/4d/crazy.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/4d/crazy.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/19/hate.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/19/hate.gif" width='16' height='16'>",   "<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/9c/tz_org.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/9c/tz_org.gif" width='16' height='16'>");           var old:uint = getTimer();   str = StringUtil.ChangeFace(str,myString,rep);   var now:uint = getTimer();   trace(now-old);   trace(str);   msg.htmlText = str;   trace(TLFText);       TLFText.htmlText=str;      import flash.text.TextField;

    var str:String = "这是一个大坏蛋,[哈哈],不过他对我好好的[嘻嘻] [呵呵] ,你说是不是呢OMG[888]?";

    var msg:TextField=new TextField();msg.autoSize = TextFieldAutoSize.LEFT;msg.multiline = true;//允许多行,要<br>发挥作用就要设置msg.wordWrap = true;//自动换行,要图片能看就必须要设置msg.width = 200; msg.height = 200;msg.border=true; addChild(msg);var myString:Array=new Array();myString.push("[呵呵]","[嘻嘻]","[哈哈]","[爱你]","[晕]","[泪]","[馋嘴]","[抓狂]","[哼]","[可爱]");

    //表情地址var rep:Array=new Array();rep.push("<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/eb/smile.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/eb/smile.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/c2/tooth.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/c2/tooth.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/6a/laugh.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/6a/laugh.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/7e/love.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/7e/love.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/a4/dizzy.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/a4/dizzy.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/d8/sad.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/d8/sad.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/b8/cz_org.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/b8/cz_org.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/4d/crazy.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/4d/crazy.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/19/hate.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/19/hate.gif" width='16' height='16'>","<img src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/9c/tz_org.gif" mce_src="http://img.t.sinajs.cn/t3/style/images/common/face/ext/normal/9c/tz_org.gif" width='16' height='16'>");

     

    var old:uint = getTimer();str = StringUtil.ChangeFace(str,myString,rep);var now:uint = getTimer();trace(now-old);trace(str);msg.htmlText = str;trace(TLFText); TLFText.htmlText=str; 

       view plaincopy to clipboardprint?package    {       public  final class StringUtil       {             public  static function ChangeFace(str:String,faceString:Array,urlArray:Array):String           {               var newstr:String = "";               //定义正则法规,匹配中括号里面汉字               var myPattern:RegExp=//[([/u4e00-/u9fa5]*)/]/g;               //定义搜索字符串,替换数组                 if (myPattern.test(str))               {                   //定义搜索到匹配数组保存起来                                       var pattern:Array = str.match(myPattern);                   //使用双循环替换中文字符串                   for (var i:int=0; i<pattern.length; i++)                   {                       //替换相应的标签,先是找到在myString中的表情的编号,然后找到rep数组对应的图片进行替换                       var index:int = faceString.indexOf(pattern[i]);                       if (index != -1)                       {                           str = str.replace(pattern[i],urlArray[index]);                           newstr = str;                       }                       else                      {                           newstr = str;                       }                   }                     pattern = null;               }               else              {                   return str;               }               return newstr;             }       }     } 

    本文来自博客,转载请标明出处:http://blog.csdn.net/hero82748274/archive/2011/01/13/6136847.aspx

    最新回复(0)