当前位置 : 主页 > 手机开发 > 其它 >

cocos 3.10 ccui.RichText修改计算行宽的计算方法

来源:互联网 收集:自由互联 发布时间:2021-06-13
由于原来富文本不区分字符和数字和汉字的大小,统一按照一个宽度来计算,这样会发现带有数字的行不排满就换行的错误所以增加了以下代码 在UIRichText.js文件中 修改函数_handleTextR

由于原来富文本不区分字符和数字和汉字的大小,统一按照一个宽度来计算,这样会发现带有数字的行不排满就换行的错误所以增加了以下代码

在UIRichText.js文件中 修改函数_handleTextRenderer(),增加绿色部分的代码


_handleTextRenderer: function (text, fontNameOrFontDef, fontSize, color) {
    if(text === "")
        return;

    if(text === "\n"){ //Force Line Breaking
        this._addNewLine();
        return;
    }

    var textRenderer = fontNameOrFontDef instanceof cc.FontDefinition ? new cc.LabelTTF(text, fontNameOrFontDef) : new cc.LabelTTF(text, fontNameOrFontDef, fontSize);
    var textRendererWidth = textRenderer.getContentSize().width;
    this._leftSpaceWidth -= textRendererWidth;
    if (this._leftSpaceWidth < 0) {
        var overstepPercent = (-this._leftSpaceWidth) / textRendererWidth;
        var curText = text;
        var stringLength = curText.length;
        var leftLength = stringLength * (1 - overstepPercent);
        var leftWords = curText.substr(0, leftLength);
        //2017-7-8 修改引擎 富文本每行计算宽度(原来是不区分字符和数字的)
 if(textRenderer && textRenderer._renderCmd && textRenderer._renderCmd._measure){
var textWidth = textRenderer._renderCmd._measure(leftWords); var maxCheckTurn = 0; while(textWidth < this._customSize.width && leftLength < stringLength && maxCheckTurn <= 100){ leftLength++; leftWords = curText.substr(0, leftLength); textWidth = textRenderer._renderCmd._measure(leftWords); maxCheckTurn++; } maxCheckTurn = 0; while(textWidth > this._customSize.width && leftLength > 1 && maxCheckTurn <= 100){ leftLength--; leftWords = curText.substr(0, leftLength); textWidth = textRenderer._renderCmd._measure(leftWords); maxCheckTurn++; } maxCheckTurn = 0;
}     }
        //修改完毕

        var cutWords = curText.substr(leftLength, curText.length - 1);
        var validLeftLength = leftLength > 0;

        if(this._lineBreakOnSpace){
            var lastSpaceIndex = leftWords.lastIndexOf(' ');
            leftLength = lastSpaceIndex === -1 ? leftLength : lastSpaceIndex+1 ;
            cutWords = curText.substr(leftLength, curText.length - 1);
            validLeftLength = leftLength > 0 && cutWords !== " ";
        }

        if (validLeftLength) {
            var leftRenderer = null;
            if( fontNameOrFontDef instanceof cc.FontDefinition)
            {
                leftRenderer = new cc.LabelTTF(leftWords.substr(0, leftLength), fontNameOrFontDef);
                leftRenderer.setOpacity(fontNameOrFontDef.fillStyle.a); //TODO: Verify that might not be needed...  }else{
                leftRenderer =  new cc.LabelTTF(leftWords.substr(0, leftLength), fontNameOrFontDef, fontSize);
                leftRenderer.setColor(color);
                leftRenderer.setOpacity(color.a);
            }
            this._pushToContainer(leftRenderer);
        }

        this._addNewLine();
        this._handleTextRenderer(cutWords, fontNameOrFontDef, fontSize, color);
    } else {
        if( fontNameOrFontDef instanceof cc.FontDefinition) {
            textRenderer.setOpacity(fontNameOrFontDef.fillStyle.a); //TODO: Verify that might not be needed...  }else {
            textRenderer.setColor(color);
            textRenderer.setOpacity(color.a);
        }
        this._pushToContainer(textRenderer);
    }
},
网友评论