Skip to content

Commit 73dc515

Browse files
committed
Merge pull request #2641 from dingpinglv/Iss2024_TextStyle
Fixed #2024: Added font-style and font-weight functions to cc.LabelTTF
2 parents ca775bc + 2b1267f commit 73dc515

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

cocos2d/core/labelttf/CCLabelTTF.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
9696

9797
_lineHeight: 0,
9898

99+
//for web
100+
_fontStyle: "normal",
101+
_fontWeight: "normal",
102+
99103
/**
100104
* Initializes the cc.LabelTTF with a font name, alignment, dimension and font size, do not call it by yourself,
101105
* you should pass the correct arguments in constructor to initialize the label.
@@ -126,7 +130,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
126130
this._vAlignment = vAlignment;
127131

128132
this._fontSize = fontSize;
129-
this._renderCmd._setFontStyle(this._fontName, fontSize);
133+
this._renderCmd._setFontStyle(this._fontName, fontSize, this._fontStyle, this._fontWeight);
130134
this.string = strInfo;
131135
this._renderCmd._setColorsString();
132136
this._renderCmd._updateTexture();
@@ -500,7 +504,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
500504

501505
this._fontName = textDefinition.fontName;
502506
this._fontSize = textDefinition.fontSize || 12;
503-
this._renderCmd._setFontStyle(this._fontName, this._fontSize)
507+
this._renderCmd._setFontStyle(this._fontName, this._fontSize, this._fontStyle, this._fontWeight);
504508

505509
// shadow
506510
if (textDefinition.shadowEnabled)
@@ -666,7 +670,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
666670
setFontSize: function (fontSize) {
667671
if (this._fontSize !== fontSize) {
668672
this._fontSize = fontSize;
669-
this._renderCmd._setFontStyle(this._fontName, this._fontSize);
673+
this._renderCmd._setFontStyle(this._fontName, this._fontSize, this._fontStyle, this._fontWeight);
670674
// Force update
671675
this._setUpdateTextureDirty();
672676
}
@@ -679,7 +683,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
679683
setFontName: function (fontName) {
680684
if (this._fontName && this._fontName != fontName) {
681685
this._fontName = fontName;
682-
this._renderCmd._setFontStyle(this._fontName, this._fontSize);
686+
this._renderCmd._setFontStyle(this._fontName, this._fontSize, this._fontStyle, this._fontWeight);
683687
// Force update
684688
this._setUpdateTextureDirty();
685689
}
@@ -693,7 +697,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
693697
if (res) {
694698
this._fontSize = parseInt(res[1]);
695699
this._fontName = res[2];
696-
this._renderCmd._setFontStyle(this._fontName, this._fontSize);
700+
this._renderCmd._setFontStyle(this._fontName, this._fontSize, this._fontStyle, this._fontWeight);
697701

698702
// Force update
699703
this._setUpdateTextureDirty();
@@ -731,6 +735,31 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
731735
return new cc.LabelTTF.CanvasRenderCmd(this);
732736
else
733737
return new cc.LabelTTF.WebGLRenderCmd(this);
738+
},
739+
740+
//For web only
741+
_setFontStyle: function(fontStyle){
742+
if (this._fontStyle != fontStyle) {
743+
this._fontStyle = fontStyle;
744+
this._renderCmd._setFontStyle(this._fontName, this._fontSize, this._fontStyle, this._fontWeight);
745+
this._setUpdateTextureDirty();
746+
}
747+
},
748+
749+
_getFontStyle: function(){
750+
return this._fontStyle;
751+
},
752+
753+
_setFontWeight: function(fontWeight){
754+
if (this._fontWeight != fontWeight) {
755+
this._fontWeight = fontWeight;
756+
this._renderCmd._setFontStyle(this._fontName, this._fontSize, this._fontStyle, this._fontWeight);
757+
this._setUpdateTextureDirty();
758+
}
759+
},
760+
761+
_getFontWeight: function(){
762+
return this._fontWeight;
734763
}
735764
});
736765

cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
7272
return this._labelContext;
7373
};
7474

75-
proto._setFontStyle = function (fontName, fontSize) {
76-
this._fontStyleStr = fontSize + "px '" + fontName + "'";
75+
proto._setFontStyle = function (fontName, fontSize, fontStyle, fontWeight) {
76+
this._fontStyleStr = fontStyle + " " + fontWeight + " " + fontSize + "px '" + fontName + "'";
7777
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, fontSize);
7878
};
7979

@@ -161,21 +161,24 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
161161
//get offset for stroke and shadow
162162
if (locDimensionsWidth === 0) {
163163
if (this._isMultiLine)
164-
locSize = cc.size(0 | (Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
165-
0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
164+
locSize = cc.size(Math.ceil(Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
165+
Math.ceil((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
166166
else
167-
locSize = cc.size(0 | (this._measure(node._string) + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
167+
locSize = cc.size(Math.ceil(this._measure(node._string) + locStrokeShadowOffsetX), Math.ceil(this._fontClientHeight + locStrokeShadowOffsetY));
168168
} else {
169169
if (node._dimensions.height === 0) {
170170
if (this._isMultiLine)
171-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((node.getLineHeight() * this._strings.length) + locStrokeShadowOffsetY));
171+
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil((node.getLineHeight() * this._strings.length) + locStrokeShadowOffsetY));
172172
else
173-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (node.getLineHeight() + locStrokeShadowOffsetY));
173+
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil(node.getLineHeight() + locStrokeShadowOffsetY));
174174
} else {
175175
//dimension is already set, contentSize must be same as dimension
176-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (node._dimensions.height + locStrokeShadowOffsetY));
176+
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil(node._dimensions.height + locStrokeShadowOffsetY));
177177
}
178178
}
179+
if(node._getFontStyle() != "normal"){ //add width for 'italic' and 'oblique'
180+
locSize.width = Math.ceil(locSize.width + node._fontSize * 0.3);
181+
}
179182
node.setContentSize(locSize);
180183
node._strokeShadowOffsetX = locStrokeShadowOffsetX;
181184
node._strokeShadowOffsetY = locStrokeShadowOffsetY;

0 commit comments

Comments
 (0)