cocos creator 知识点记录二:富文本打字昵称截断

Posted 雨尘无痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos creator 知识点记录二:富文本打字昵称截断相关的知识,希望对你有一定的参考价值。

知乎链接:

Cocos Creator系列一: 导入DragonBones动画:https://zhuanlan.zhihu.com/p/59294829

Cocos Creator系列二:基础知识(持续更新):https://zhuanlan.zhihu.com/p/59567183

Cocos Creator系列三:网络同步机制,网络协议(持续更新):https://zhuanlan.zhihu.com/p/60883576

Cocos Creator系列四:消息框架:https://zhuanlan.zhihu.com/p/63193440

富文本:

文案里配富文本标签,

//str  字符串  index 需要获取index+1长度文字并加标签   
 public static getTagStrIndex(str:string,index:number):string
    
        var tagIndex1 = str.indexOf('<',0);
        var tagIndex2 = str.indexOf('>',0);
        var html = "";

        if(index>=this.getTagStrLen(str))
            return "";

        var tagStack: Array<string> = new Array<string>();
        var i = 0;

        if(index<tagIndex1||tagIndex1==-1)
        
            //没有标签
            return  str.substr(0,index+1);
        else
        
            //有标签
            var curLen = 0;
            var curIndex = 0;
            // html += str.substr(0,tagIndex1);
            while(curLen<=index)
            
                var needLen = index-curLen+1;
                if(tagIndex1!=-1&&needLen<=(tagIndex1-curIndex))
                
                    //长度满足
                    for(i=0;i<tagStack.length;i++)
                    
                        html += tagStack[i];
                    
                    html += str.substr(curIndex,needLen);
                    for(i=tagStack.length-1;tagStack.length>0&&i>=0;i--)
                    
                        if(tagStack[i][1]=='c')
                            html += "</color>";
                        else if(tagStack[i][1]=='b')
                            html += "</b>";
                    
                    return html;
                else if(tagIndex1==-1)
                
                    //没有标签
                    var needLen = index-curLen+1;
                    html += str.substr(curIndex,needLen);
                    return html;
                

                for(i=0;i<tagStack.length;i++)
                
                    html += tagStack[i];
                
                html += str.substr(curIndex,tagIndex1-curIndex);
                for(i=tagStack.length-1;i>=0;i--)
                
                    if(tagStack[i][1]=='c')
                        html += "</color>";
                    else if(tagStack[i][1]=='b')
                        html += "</b>";
                

                curLen += tagIndex1-curIndex;
                curIndex = tagIndex2+1;

                var tag = str.substring(tagIndex1,tagIndex2+1);
                if(tag[1]=='i')
                
                    curLen += 1;
                    html += tag;
                else if(tag[1]=='b'||tag[1]=='c')
                    tagStack.push(tag);
                else if(tag[1]=='/')
                
                    tagStack.pop();
                

                tagIndex1 = str.indexOf('<',curIndex);
                tagIndex2 = str.indexOf('>',curIndex);

            
        
    

打字效果:

    TypingWrite(label:cc.RichText,text:string)
    
        if(this.typingWriteState)
        
            label.string = this.blackString(text);
            this.typingWriteState = false;
            return;
        
        this.typingWriteState = true;
        let len = GlobalFunction.getTagStrLen(text);
        let html = "";
        let step = 0;
        this.func = function()
            html = GlobalFunction.getTagStrIndex(text,step);
            label.string = this.blackString(html);
            if(++step == len)
                this.unschedule(this.func,this);
                label.string = this.blackString(text);
                this.typingWriteState = false;
            else if(this.typingWriteState==false)
            
                this.unschedule(this.func,this);
                label.string = this.blackString(text);
            
        
        this.schedule(this.func,0.05,cc.macro.REPEAT_FOREVER,0);
        this.questionLabel.node.active = true;
        label.string = "";
    

昵称截断

原文:https://forum.cocos.com/t/cocos-creator/76916

 

以上是关于cocos creator 知识点记录二:富文本打字昵称截断的主要内容,如果未能解决你的问题,请参考以下文章

Cocos Creator 知识点记录

Cocos Creator 知识点记录

cocos creator 2.1 文本渲染分析。为啥创建Label性能差?

cocos creator 3.2.0 聊天富文本效果

Cocos Creator游戏开发中单例的实现

cocos creator blink闪烁动画怎么没有效果