Skip to content

Commit 84c2668

Browse files
committed
Merge pull request cocos2d#2444 from VisualSJ/develop-lineHeight
Issue cocos2d#2024: LabelTTF add setLineHeight
2 parents b67bb36 + ccd1f75 commit 84c2668

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cocos2d/core/labelttf/CCLabelTTF.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
101101
_lineWidths: null,
102102
_className: "LabelTTF",
103103

104+
_lineHeight: 0,
105+
104106
/**
105107
* Initializes the cc.LabelTTF with a font name, alignment, dimension and font size, do not call it by yourself,
106108
* you should pass the correct arguments in constructor to initialize the label.
@@ -215,6 +217,14 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
215217
this._setColorsString();
216218
},
217219

220+
getLineHiehgt: function(){
221+
return this._lineHeight || this._fontClientHeight;
222+
},
223+
224+
setLineHeight: function(lineHeight){
225+
this._lineHeight = lineHeight;
226+
},
227+
218228
/**
219229
* Returns the text of the label
220230
* @return {String}
@@ -752,6 +762,11 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
752762
context.textAlign = cc.LabelTTF._textAlign[locHAlignment];
753763

754764
var locContentWidth = this._contentSize.width - locStrokeShadowOffsetX;
765+
766+
//lineHiehgt
767+
var lineHeight = this.getLineHiehgt();
768+
var transformTop = (lineHeight - this._fontClientHeight) / 2;
769+
755770
if (locHAlignment === cc.TEXT_ALIGNMENT_RIGHT)
756771
xOffset += locContentWidth;
757772
else if (locHAlignment === cc.TEXT_ALIGNMENT_CENTER)
@@ -761,13 +776,13 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
761776
if (this._isMultiLine) {
762777
var locStrLen = this._strings.length;
763778
if (locVAlignment === cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM)
764-
yOffset = locFontHeight + locContentSizeHeight - locFontHeight * locStrLen;
779+
yOffset = lineHeight - transformTop * 2 + locContentSizeHeight - lineHeight * locStrLen;
765780
else if (locVAlignment === cc.VERTICAL_TEXT_ALIGNMENT_CENTER)
766-
yOffset = locFontHeight / 2 + (locContentSizeHeight - locFontHeight * locStrLen) / 2;
781+
yOffset = (lineHeight - transformTop * 2) / 2 + (locContentSizeHeight - lineHeight * locStrLen) / 2;
767782

768783
for (var i = 0; i < locStrLen; i++) {
769784
var line = this._strings[i];
770-
var tmpOffsetY = -locContentSizeHeight + (locFontHeight * i) + yOffset;
785+
var tmpOffsetY = -locContentSizeHeight + (lineHeight * i + transformTop) + yOffset;
771786
if (locStrokeEnabled)
772787
context.strokeText(line, xOffset, tmpOffsetY);
773788
context.fillText(line, xOffset, tmpOffsetY);
@@ -923,9 +938,9 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
923938
} else {
924939
if (this._dimensions.height === 0) {
925940
if (this._isMultiLine)
926-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
941+
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((this.getLineHiehgt() * this._strings.length) + locStrokeShadowOffsetY));
927942
else
928-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
943+
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this.getLineHiehgt() + locStrokeShadowOffsetY));
929944
} else {
930945
//dimension is already set, contentSize must be same as dimension
931946
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this._dimensions.height + locStrokeShadowOffsetY));

0 commit comments

Comments
 (0)