由于原来富文本不区分字符和数字和汉字的大小,统一按照一个宽度来计算,这样会发现带有数字的行不排满就换行的错误所以增加了以下代码
在UIRichText.js文件中 修改函数_handleTextRenderer(),增加绿色部分的代码
_handleTextRenderer: function (text,fontNameOrFontDeffontSizecolor) { if(text === "") return; "\n"){ //Force Line Breaking this._addNewLine() } var textRenderer = fontNameOrFontDef instanceof cc.FontDefinition ? new cc.LabelTTF(textfontNameOrFontDef) : fontSize)var textRendererWidth = textRenderer.getContentSize().widththis._leftSpaceWidth -= textRendererWidthif (_leftSpaceWidth < 0) { var overstepPercent = (-_leftSpaceWidth) / textRendererWidthvar curText = textvar stringLength = curText.lengthvar leftLength = stringLength * (1 - overstepPercent)var leftWords = curText.substr(0leftLength) //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(leftLengthcurText.length - 1)var validLeftLength = leftLength > if(_lineBreakOnSpace){ var lastSpaceIndex = leftWords.lastIndexOf(' ') leftLength = lastSpaceIndex === -1 ? leftLength : lastSpaceIndex+1 cutWords = curText. validLeftLength = leftLength > 0 && cutWords !== " " } if (validLeftLength) { var leftRenderer = null if( fontNameOrFontDef FontDefinition) { leftRenderer = LabelTTF(leftWords.fontNameOrFontDef) leftRenderer.setOpacity(fontNameOrFontDef.fillStyle.a); //TODO: Verify that might not be needed... }else{ leftRenderer = setColor(color)setOpacity(color. } _pushToContainer(leftRenderer)this._handleTextRenderer(cutWordscolor) } else { FontDefinition) { textRenderer.else { textRenderer. textRenderer. } _pushToContainer(textRenderer) } } 原文链接:/cocos2dx/338544.html