Skip to content

Commit d075b6f

Browse files
author
pandamicro
committed
Improve label adaptation performance
1 parent 2e93edc commit d075b6f

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

cocos2d/core/labelttf/CCLabelTTF.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
133133
this._renderCmd._setFontStyle(this._fontName, fontSize, this._fontStyle, this._fontWeight);
134134
this.string = strInfo;
135135
this._renderCmd._setColorsString();
136-
this._renderCmd._updateTexture();
136+
if (this._string) {
137+
this._renderCmd._updateTexture();
138+
}
137139
this._setUpdateTextureDirty();
138140

139141
// Needed for high dpi text.

cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
430430
};
431431

432432
proto._measure = function (text) {
433-
return this._labelContext.measureText(text).width;
433+
if (text) {
434+
return this._labelContext.measureText(text).width;
435+
} else {
436+
return 0;
437+
}
434438
};
435439

436440
})();
@@ -462,9 +466,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
462466
};
463467

464468
proto._measure = function (text) {
465-
var context = cc._renderContext.getContext();
466-
context.font = this._fontStyleStr;
467-
return context.measureText(text).width;
469+
if(text) {
470+
var context = cc._renderContext.getContext();
471+
context.font = this._fontStyleStr;
472+
return context.measureText(text).width;
473+
} else {
474+
return 0;
475+
}
468476
};
469477

470478
proto._updateTexture = function () {

cocos2d/labels/CCLabelBMFont.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
128128
}
129129
}
130130
if (this._textureLoaded) {
131-
this.createFontChars();
131+
if(this._string && this._string.length > 0) {
132+
this.createFontChars();
133+
}
132134
if (needUpdateLabel)
133135
this.updateLabel();
134136
}
@@ -273,6 +275,9 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
273275
* updates the font chars based on the string to render
274276
*/
275277
createFontChars: function () {
278+
var locStr = this._string;
279+
var stringLen = locStr ? locStr.length : 0;
280+
276281
var self = this;
277282
var cmd = this._renderCmd;
278283
var locTexture = cmd._texture || this._texture;
@@ -285,11 +290,6 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
285290

286291
var quantityOfLines = 1;
287292

288-
var locStr = self._string;
289-
var stringLen = locStr ? locStr.length : 0;
290-
291-
if (stringLen === 0)
292-
return;
293293

294294
var i, locCfg = self._config, locKerningDict = locCfg.kerningDict,
295295
locCommonH = locCfg.commonHeight, locFontDict = locCfg.fontDefDictionary;
@@ -383,13 +383,17 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
383383
var self = this;
384384
var locChildren = self._children;
385385
if (locChildren) {
386-
for (var i = 0, li = locChildren.length; i < li; i++) {
386+
var length = locChildren.length;
387+
for (var i = 0, li = length; i < li; i++) {
387388
var node = locChildren[i];
388389
if (node) node.visible = false;
389390
}
390391
}
391-
if (self._config)
392-
self.createFontChars();
392+
if (self._config) {
393+
if(self._string && self._string.length > 0) {
394+
self.createFontChars();
395+
}
396+
}
393397

394398
if (!fromUpdate)
395399
self.updateLabel();
@@ -715,15 +719,20 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
715719
var self1 = this;
716720
self1._textureLoaded = true;
717721
self1.setTexture(sender);
718-
self1.createFontChars();
722+
if(self1._string && self1._string.length > 0) {
723+
self1.createFontChars();
724+
}
725+
719726
self1._changeTextureColor();
720727
self1.updateLabel();
721728

722729
self1.dispatchEvent("load");
723730
}, self);
724731
} else {
725732
self.setTexture(texture);
726-
self.createFontChars();
733+
if(self._string && self._string.length > 0) {
734+
self.createFontChars();
735+
}
727736
}
728737
}
729738
},

0 commit comments

Comments
 (0)