Skip to content

Commit 92f7f8a

Browse files
committed
Fix UIText issue and Label getContentSize value wrong in retina mode
1 parent 99c7025 commit 92f7f8a

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

Diff for: cocos2d/core/labelttf/CCLabelTTF.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
612612
setScale: function (scale, scaleY) {
613613
this._scaleX = scale / cc.view.getDevicePixelRatio();
614614
this._scaleY = ((scaleY || scaleY === 0) ? scaleY : scale) /
615-
cc.view.getDevicePixelRatio();
615+
cc.view.getDevicePixelRatio();
616616
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
617617
},
618618

@@ -806,18 +806,18 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
806806
getContentSize: function () {
807807
if (this._needUpdateTexture)
808808
this._renderCmd._updateTTF();
809-
return cc.Sprite.prototype.getContentSize.call(this);
809+
return cc.size(this._contentSize);
810810
},
811811

812812
_getWidth: function () {
813813
if (this._needUpdateTexture)
814814
this._renderCmd._updateTTF();
815-
return cc.Sprite.prototype._getWidth.call(this);
815+
return this._contentSize.width;
816816
},
817817
_getHeight: function () {
818818
if (this._needUpdateTexture)
819819
this._renderCmd._updateTTF();
820-
return cc.Sprite.prototype._getHeight.call(this);
820+
return this._contentSize.height;
821821
},
822822

823823
setTextureRect: function (rect, rotated, untrimmedSize) {

Diff for: cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js

+32-21
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
4747
this._isMultiLine = false;
4848
this._status = [];
4949
this._renderingIndex = 0;
50+
51+
this._texRect = cc.rect();
5052
};
5153
var proto = cc.LabelTTF.RenderCmd.prototype;
5254
proto.constructor = cc.LabelTTF.RenderCmd;
@@ -92,7 +94,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
9294

9395
proto._updateTTF = function () {
9496
var node = this._node;
95-
var locDimensionsWidth = node._dimensions.width, i, strLength;
97+
var pixelRatio = cc.view.getDevicePixelRatio();
98+
var locDimensionsWidth = node._dimensions.width * pixelRatio, i, strLength;
9699
var locLineWidth = this._lineWidths;
97100
locLineWidth.length = 0;
98101

@@ -124,7 +127,6 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
124127
locStrokeShadowOffsetY += Math.abs(locOffsetSize.y) * 2;
125128
}
126129

127-
var pixelRatio = cc.view.getDevicePixelRatio();
128130
//get offset for stroke and shadow
129131
if (locDimensionsWidth === 0) {
130132
if (this._isMultiLine)
@@ -155,25 +157,35 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
155157
if(node._getFontStyle() !== "normal"){ //add width for 'italic' and 'oblique'
156158
locSize.width = Math.ceil(locSize.width + node._fontSize * 0.3);
157159
}
158-
node.setContentSize(locSize);
160+
if (this._strings.length === 0) {
161+
this._texRect.width = 1;
162+
this._texRect.height = locSize.height || 1;
163+
}
164+
else {
165+
this._texRect.width = locSize.width;
166+
this._texRect.height = locSize.height;
167+
}
168+
var nodeW = locSize.width / pixelRatio, nodeH = locSize.height / pixelRatio;
169+
node.setContentSize(nodeW, nodeH);
159170
node._strokeShadowOffsetX = locStrokeShadowOffsetX;
160171
node._strokeShadowOffsetY = locStrokeShadowOffsetY;
161172

162173
// need computing _anchorPointInPoints
163174
var locAP = node._anchorPoint;
164175
this._anchorPointInPoints.x = (locStrokeShadowOffsetX * 0.5) + ((locSize.width - locStrokeShadowOffsetX) * locAP.x);
165-
this._anchorPointInPoints.y = (locStrokeShadowOffsetY * 0.5) + ((locSize.height - locStrokeShadowOffsetY) * locAP.y);
176+
this._anchorPointInPoints.y = (locStrokeShadowOffsetY * 0.5) + ((locSize.width - locStrokeShadowOffsetY) * locAP.y);
166177
};
167178

168179
proto._saveStatus = function () {
169180
var node = this._node;
181+
var scale = cc.view.getDevicePixelRatio();
170182
var locStrokeShadowOffsetX = node._strokeShadowOffsetX, locStrokeShadowOffsetY = node._strokeShadowOffsetY;
171-
var locContentSizeHeight = node._contentSize.height - locStrokeShadowOffsetY, locVAlignment = node._vAlignment,
183+
var locContentSizeHeight = node._contentSize.height * scale - locStrokeShadowOffsetY, locVAlignment = node._vAlignment,
172184
locHAlignment = node._hAlignment;
173185
var dx = locStrokeShadowOffsetX * 0.5,
174186
dy = locContentSizeHeight + locStrokeShadowOffsetY * 0.5;
175187
var xOffset = 0, yOffset = 0, OffsetYArray = [];
176-
var locContentWidth = node._contentSize.width - locStrokeShadowOffsetX;
188+
var locContentWidth = node._contentSize.width * scale - locStrokeShadowOffsetX;
177189

178190
//lineHeight
179191
var lineHeight = node.getLineHeight();
@@ -301,7 +313,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
301313
var flags = cc.Node._dirtyFlags, locFlag = this._dirtyFlag;
302314

303315
cc.Node.RenderCmd.prototype.updateStatus.call(this);
304-
316+
305317
if (locFlag & flags.textDirty)
306318
this._updateTexture();
307319

@@ -313,9 +325,9 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
313325

314326
proto._syncStatus = function (parentCmd) {
315327
var flags = cc.Node._dirtyFlags, locFlag = this._dirtyFlag;
316-
328+
317329
cc.Node.RenderCmd.prototype._syncStatus.call(this, parentCmd);
318-
330+
319331
if (locFlag & flags.textDirty)
320332
this._updateTexture();
321333

@@ -371,6 +383,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
371383
locCanvas.width = 1;
372384
locCanvas.height = 1;
373385
this._labelContext = locCanvas.getContext("2d");
386+
this._texRect = cc.rect();
374387
};
375388

376389
cc.LabelTTF.CacheRenderCmd.prototype = Object.create( cc.LabelTTF.RenderCmd.prototype);
@@ -382,9 +395,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
382395
proto._updateTexture = function () {
383396
this._dirtyFlag = this._dirtyFlag & cc.Node._dirtyFlags.textDirty ^ this._dirtyFlag;
384397
var node = this._node;
385-
var locContentSize = node._contentSize;
386398
this._updateTTF();
387-
var width = locContentSize.width, height = locContentSize.height;
399+
var width = this._texRect.width, height = this._texRect.height;
388400

389401
var locContext = this._labelContext, locLabelCanvas = this._labelCanvas;
390402

@@ -395,24 +407,24 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
395407
}
396408

397409
if (node._string.length === 0) {
398-
locLabelCanvas.width = 1;
399-
locLabelCanvas.height = locContentSize.height || 1;
410+
locLabelCanvas.width = width;
411+
locLabelCanvas.height = height;
400412
node._texture && node._texture.handleLoadedTexture();
401-
node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height));
413+
node.setTextureRect(this._texRect);
402414
return true;
403415
}
404416

405417
//set size for labelCanvas
406418
locContext.font = this._fontStyleStr;
407419

408420
var flag = locLabelCanvas.width === width && locLabelCanvas.height === height;
409-
locLabelCanvas.width = width;
410-
locLabelCanvas.height = height;
421+
locLabelCanvas.width = this._texRect.width;
422+
locLabelCanvas.height = this._texRect.height;
411423
if (flag) locContext.clearRect(0, 0, width, height);
412424
this._saveStatus();
413425
this._drawTTFInCanvas(locContext);
414426
node._texture && node._texture.handleLoadedTexture();
415-
node.setTextureRect(cc.rect(0, 0, width, height));
427+
node.setTextureRect(this._texRect);
416428
return true;
417429
};
418430

@@ -460,15 +472,14 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
460472
proto._updateTexture = function () {
461473
this._dirtyFlag = this._dirtyFlag & cc.Node._dirtyFlags.textDirty ^ this._dirtyFlag;
462474
var node = this._node;
463-
var locContentSize = node._contentSize;
475+
var scale = cc.view.getDevicePixelRatio();
464476
this._updateTTF();
465-
var width = locContentSize.width, height = locContentSize.height;
466477
if (node._string.length === 0) {
467-
node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height));
478+
node.setTextureRect(this._texRect);
468479
return true;
469480
}
470481
this._saveStatus();
471-
node.setTextureRect(cc.rect(0, 0, width, height));
482+
node.setTextureRect(this._texRect);
472483
return true;
473484
};
474485

Diff for: extensions/ccui/uiwidgets/UIText.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
4646
_fontName: "Arial",
4747
_fontSize: 16,
4848
_onSelectedScaleOffset:0.5,
49-
_labelRenderer: "",
49+
_labelRenderer: null,
5050
_textAreaSize: null,
5151
_textVerticalAlignment: 0,
5252
_textHorizontalAlignment: 0,

0 commit comments

Comments
 (0)