CCTLText.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. CTLText.prototype = {
  2. constructor : CTLText,
  3. __autofit : function(){
  4. if(this._bFitText){
  5. var iFontSize = this._iFontSize;
  6. while(
  7. this._oText.getBounds().height > (this._iHeight -this._iPaddingV*2) ||
  8. this._oText.getBounds().width > (this._iWidth-this._iPaddingH*2)
  9. ){
  10. iFontSize--;
  11. this._oText.font = iFontSize+"px "+this._szFont;
  12. this._oText.lineHeight = Math.round(iFontSize*this._fLineHeightFactor);
  13. this.__updateY();
  14. this.__verticalAlign();
  15. if ( iFontSize < 8 ){
  16. break;
  17. }
  18. };
  19. this._iFontSize = iFontSize;
  20. }
  21. //trace(this._oText.text + "-->fontsizedebug:"+iFontSize);
  22. },
  23. __verticalAlign : function(){
  24. if(this._bVerticalAlign){
  25. var iCurHeight = this._oText.getBounds().height;
  26. this._oText.y -= (iCurHeight-this._iHeight)/2 + (this._iPaddingV);
  27. }
  28. },
  29. __updateY : function(){
  30. this._oText.y = this._y + this._iPaddingV;
  31. switch(this._oText.textBaseline){
  32. case "middle":{
  33. this._oText.y += (this._oText.lineHeight/2) +
  34. (this._iFontSize*this._fLineHeightFactor-this._iFontSize);
  35. }break;
  36. }
  37. },
  38. __createText : function(szMsg){
  39. if (this._bDebug){
  40. this._oDebugShape = new createjs.Shape();
  41. this._oDebugShape.graphics.beginFill("rgba(255,0,0,0.5)").drawRect(
  42. this._x, this._y, this._iWidth, this._iHeight);
  43. this._oContainer.addChild(this._oDebugShape);
  44. }
  45. this._oText = new createjs.Text(szMsg, this._iFontSize+"px "+this._szFont, this._szColor);
  46. this._oText.textBaseline = "middle";
  47. this._oText.lineHeight = Math.round(this._iFontSize*this._fLineHeightFactor);
  48. this._oText.textAlign = this._szAlign;
  49. if ( this._bMultiline ){
  50. this._oText.lineWidth = this._iWidth - (this._iPaddingH*2);
  51. }else{
  52. this._oText.lineWidth = null;
  53. }
  54. switch(this._szAlign){
  55. case "center":{
  56. this._oText.x = this._x+(this._iWidth/2);
  57. }break;
  58. case "left":{
  59. this._oText.x = this._x+this._iPaddingH;
  60. }break;
  61. case "right":{
  62. this._oText.x = this._x+this._iWidth-this._iPaddingH;
  63. }break;
  64. }
  65. this._oContainer.addChild(this._oText);
  66. this.refreshText(szMsg);
  67. },
  68. unload : function(){
  69. this._oContainer.removeChild(this._oText);
  70. },
  71. setVerticalAlign : function( bVerticalAlign ){
  72. this._bVerticalAlign = bVerticalAlign;
  73. },
  74. setOutline : function(iSize){
  75. if ( this._oText !== null ){
  76. this._oText.outline = iSize;
  77. }
  78. },
  79. setShadow : function(szColor,iOffsetX,iOffsetY,iBlur){
  80. if ( this._oText !== null ){
  81. this._oText.shadow = new createjs.Shadow(szColor, iOffsetX,iOffsetY,iBlur);
  82. }
  83. },
  84. setColor : function(szColor){
  85. this._oText.color = szColor;
  86. },
  87. setAlpha : function(iAlpha){
  88. this._oText.alpha = iAlpha;
  89. },
  90. setX : function(iNewX){
  91. this._oText.x = iNewX;
  92. this._x = iNewX;
  93. },
  94. setY : function(iNewY){
  95. this._oText.y = iNewY;
  96. this._y = iNewY;
  97. },
  98. removeTweens : function(){
  99. createjs.Tween.removeTweens(this._oText);
  100. },
  101. getText : function(){
  102. return this._oText;
  103. },
  104. getY : function(){
  105. return this._y;
  106. },
  107. getFontSize : function(){
  108. return this._iFontSize;
  109. },
  110. refreshText : function(szMsg){
  111. if(szMsg === ""){
  112. szMsg = " ";
  113. }
  114. if ( this._oText === null ){
  115. this.__createText(szMsg);
  116. }
  117. this._oText.text = szMsg;
  118. this._oText.font = this._iFontSize+"px "+this._szFont;
  119. this._oText.lineHeight = Math.round(this._iFontSize*this._fLineHeightFactor);
  120. this.__autofit();
  121. this.__updateY();
  122. this.__verticalAlign();
  123. }
  124. };
  125. function CTLText( oContainer,
  126. x, y, iWidth, iHeight,
  127. iFontSize, szAlign, szColor, szFont,iLineHeightFactor,
  128. iPaddingH, iPaddingV,
  129. szMsg,
  130. bFitText, bVerticalAlign, bMultiline,
  131. bDebug ){
  132. this._oContainer = oContainer;
  133. this._x = x;
  134. this._y = y;
  135. this._iWidth = iWidth;
  136. this._iHeight = iHeight;
  137. this._bMultiline = bMultiline;
  138. this._iFontSize = iFontSize;
  139. this._szAlign = szAlign;
  140. this._szColor = szColor;
  141. this._szFont = szFont;
  142. this._iPaddingH = iPaddingH;
  143. this._iPaddingV = iPaddingV;
  144. this._bVerticalAlign = bVerticalAlign;
  145. this._bFitText = bFitText;
  146. this._bDebug = bDebug;
  147. //this._bDebug = true;
  148. // RESERVED
  149. this._oDebugShape = null;
  150. this._fLineHeightFactor = iLineHeightFactor;
  151. this._oText = null;
  152. if ( szMsg ){
  153. this.__createText(szMsg);
  154. }
  155. }