CCTLText.js 5.3 KB

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