@@ -115,14 +115,12 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
115115
116116 //set size for labelCanvas
117117 this . _labelContext . font = this . _fontStyleStr ;
118+ var size = this . _computeLabelSizeForWebGL ( ) ;
118119
119- var dim = this . _labelContext . measureText ( this . _string ) ;
120- //TODO multiple line
120+ this . _labelCanvas . width = size . width ;
121+ this . _labelCanvas . height = size . height ;
121122
122- this . _labelCanvas . width = dim . width ;
123- this . _labelCanvas . height = this . _fontSize ;
124-
125- this . setContentSize ( cc . size ( this . _labelCanvas . width , this . _labelCanvas . height ) ) ;
123+ this . setContentSize ( cc . size ( this . _labelCanvas . width , this . _labelCanvas . height ) ) ;
126124
127125 //draw text to labelCanvas
128126 this . _drawTTFInCanvasForWebGL ( this . _labelContext ) ;
@@ -131,12 +129,36 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
131129 return true ;
132130 } ,
133131
134- _drawTTFInCanvasForWebGL :function ( context ) {
135- if ( ! context )
136- return ;
132+ _computeLabelSizeForWebGL :function ( ) {
133+ if ( this . _dimensions . width == 0 ) {
134+ if ( this . _string . indexOf ( "\n" ) === - 1 ) {
135+ var dim = this . _labelContext . measureText ( this . _string ) ;
136+ return cc . size ( dim . width , this . _fontSize ) ;
137+ } else {
138+ var rowHeight = this . _fontSize * 1.2 ;
139+ var tmpWords = this . _string . split ( "\n" ) ;
140+ var lineHeight = tmpWords . length ;
141+ var splitStrWidthArr = [ ] ;
142+ var maxLineWidth = 0 ;
143+ for ( var i = 0 ; i < lineHeight ; i ++ ) {
144+ splitStrWidthArr [ i ] = this . _labelContext . measureText ( tmpWords [ i ] ) . width ;
145+ if ( splitStrWidthArr [ i ] > maxLineWidth )
146+ maxLineWidth = splitStrWidthArr [ i ] ;
147+ }
148+ return cc . size ( maxLineWidth , rowHeight * lineHeight ) ;
149+ }
150+ } else {
151+
152+ }
153+
154+ } ,
155+
156+ _drawTTFInCanvasForWebGL :function ( context ) {
157+ if ( ! context )
158+ return ;
137159
138160 context . save ( ) ;
139- context . translate ( this . _contentSize . width / 2 , this . _contentSize . height / 2 ) ;
161+ context . translate ( this . _contentSize . width / 2 , this . _contentSize . height / 2 ) ;
140162 //this is fillText for canvas
141163 if ( context . font != this . _fontStyleStr )
142164 context . font = this . _fontStyleStr ;
@@ -155,9 +177,9 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
155177 context . textBaseline = "bottom" ;
156178 context . textAlign = "left" ;
157179
158- if ( this . _string . indexOf ( "\n" ) > - 1 )
180+ if ( this . _string . indexOf ( "\n" ) > - 1 ) {
159181 this . _multiLineText ( context ) ;
160- else
182+ } else
161183 context . fillText ( this . _string , - this . _contentSize . width * this . _anchorPoint . x , this . _contentSize . height * this . _anchorPoint . y ) ;
162184 } else {
163185 context . textBaseline = cc . LabelTTF . _textBaseline [ this . _vAlignment ] ;
@@ -376,10 +398,6 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
376398
377399 this . setString ( strInfo ) ;
378400
379- //TODO
380- //if (cc.renderContextType === cc.CANVAS) {
381- // this._updateTTF();
382- //}
383401 return true ;
384402 }
385403 return false ;
@@ -466,7 +484,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
466484 xOffset = centerPoint . x - maxLineWidth ;
467485 if ( this . _hAlignment == cc . TEXT_ALIGNMENT_CENTER )
468486 xOffset = maxLineWidth - splitStrWidthArr [ i ] ;
469- context . fillText ( tmpWords [ i ] , xOffset , i * rowHeight - centerPoint . y + rowHeight / 2 ) ;
487+ context . fillText ( tmpWords [ i ] , xOffset , i * rowHeight - centerPoint . y + rowHeight ) ;
470488 }
471489 } ,
472490
0 commit comments