UIText.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * The text control of Cocos UI.
  23. * @class
  24. * @extends ccui.Widget
  25. *
  26. * @property {Number} boundingWidth - Width of the bounding area of label, the real content width is limited by boundingWidth
  27. * @property {Number} boundingHeight - Height of the bounding area of label, the real content height is limited by boundingHeight
  28. * @property {String} string - The content string of the label
  29. * @property {Number} stringLength - <@readonly> The content string length of the label
  30. * @property {String} font - The label font with a style string: e.g. "18px Verdana"
  31. * @property {String} fontName - The label font name
  32. * @property {Number} fontSize - The label font size
  33. * @property {Number} textAlign - Horizontal Alignment of label, cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT
  34. * @property {Number} verticalAlign - Vertical Alignment of label: cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM
  35. * @property {Boolean} touchScaleEnabled - Indicate whether the label will scale when touching
  36. */
  37. ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
  38. _touchScaleChangeEnabled: false,
  39. _normalScaleValueX: 1,
  40. _normalScaleValueY: 1,
  41. _fontName: "Thonburi",
  42. _fontSize: 10,
  43. _onSelectedScaleOffset:0.5,
  44. _labelRenderer: "",
  45. _textAreaSize: null,
  46. _textVerticalAlignment: 0,
  47. _textHorizontalAlignment: 0,
  48. _className: "Text",
  49. _type: null,
  50. _labelRendererAdaptDirty: true,
  51. /**
  52. * allocates and initializes a UILabel.
  53. * Constructor of ccui.Text. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  54. * @param {String} textContent
  55. * @param {String} fontName
  56. * @param {Number} fontSize
  57. * @example
  58. * // example
  59. * var uiLabel = new ccui.Text();
  60. */
  61. ctor: function (textContent, fontName, fontSize) {
  62. this._type = ccui.Text.Type.SYSTEM;
  63. this._textAreaSize = cc.size(0, 0);
  64. ccui.Widget.prototype.ctor.call(this);
  65. fontSize && this.init(textContent, fontName, fontSize);
  66. },
  67. /**
  68. * Initializes a ccui.Text. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
  69. * @param {String} textContent
  70. * @param {String} fontName
  71. * @param {Number} fontSize
  72. * @returns {boolean}
  73. * @override
  74. */
  75. init: function (textContent, fontName, fontSize) {
  76. if (ccui.Widget.prototype.init.call(this)) {
  77. if(arguments.length > 0){
  78. this.setString(textContent);
  79. this.setFontName(fontName);
  80. this.setFontSize(fontSize);
  81. }
  82. return true;
  83. }
  84. return false;
  85. },
  86. _initRenderer: function () {
  87. this._labelRenderer = new cc.LabelTTF();
  88. this.addProtectedChild(this._labelRenderer, ccui.Text.RENDERER_ZORDER, -1);
  89. },
  90. /**
  91. * Changes the value of ccui.Text.
  92. * @deprecated since v3.0, please use setString() instead.
  93. * @param {String} text
  94. */
  95. setText: function (text) {
  96. cc.log("Please use the setString");
  97. this.setString(text);
  98. },
  99. /**
  100. * Changes the value of ccui.Text.
  101. * @param {String} text
  102. */
  103. setString: function (text) {
  104. this._labelRenderer.setString(text);
  105. this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
  106. this._labelRendererAdaptDirty = true;
  107. },
  108. /**
  109. * Gets the string value of ccui.Text.
  110. * @deprecated since v3.0, please use getString instead.
  111. * @returns {String}
  112. */
  113. getStringValue: function () {
  114. cc.log("Please use the getString");
  115. return this._labelRenderer.getString();
  116. },
  117. /**
  118. * Gets the string value of ccui.Text.
  119. * @returns {String}
  120. */
  121. getString: function () {
  122. return this._labelRenderer.getString();
  123. },
  124. /**
  125. * Gets the string length of ccui.Text.
  126. * @returns {Number}
  127. */
  128. getStringLength: function () {
  129. return this._labelRenderer.getStringLength();
  130. },
  131. /**
  132. * Sets fontSize
  133. * @param {Number} size
  134. */
  135. setFontSize: function (size) {
  136. this._labelRenderer.setFontSize(size);
  137. this._fontSize = size;
  138. this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
  139. this._labelRendererAdaptDirty = true;
  140. },
  141. /**
  142. * Returns font Size of ccui.Text
  143. * @returns {Number}
  144. */
  145. getFontSize: function () {
  146. return this._fontSize;
  147. },
  148. /**
  149. * Sets font name
  150. * @return {String} name
  151. */
  152. setFontName: function (name) {
  153. this._fontName = name;
  154. this._labelRenderer.setFontName(name);
  155. this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
  156. this._labelRendererAdaptDirty = true;
  157. },
  158. /**
  159. * Returns font name of ccui.Text.
  160. * @returns {string}
  161. */
  162. getFontName: function () {
  163. return this._fontName;
  164. },
  165. _setFont: function (font) {
  166. var res = cc.LabelTTF._fontStyleRE.exec(font);
  167. if (res) {
  168. this._fontSize = parseInt(res[1]);
  169. this._fontName = res[2];
  170. this._labelRenderer._setFont(font);
  171. this._labelScaleChangedWithSize();
  172. }
  173. },
  174. _getFont: function () {
  175. return this._labelRenderer._getFont();
  176. },
  177. /**
  178. * Returns the type of ccui.Text.
  179. * @returns {null}
  180. */
  181. getType: function(){
  182. return this._type;
  183. },
  184. /**
  185. * Sets text Area Size
  186. * @param {cc.Size} size
  187. */
  188. setTextAreaSize: function (size) {
  189. this._labelRenderer.setDimensions(size);
  190. this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
  191. this._labelRendererAdaptDirty = true;
  192. },
  193. /**
  194. * Returns renderer's dimension.
  195. * @returns {cc.Size}
  196. */
  197. getTextAreaSize: function(){
  198. return this._labelRenderer.getDimensions();
  199. },
  200. /**
  201. * Sets Horizontal Alignment of cc.LabelTTF
  202. * @param {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT} alignment Horizontal Alignment
  203. */
  204. setTextHorizontalAlignment: function (alignment) {
  205. this._labelRenderer.setHorizontalAlignment(alignment);
  206. this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
  207. this._labelRendererAdaptDirty = true;
  208. },
  209. /**
  210. * Returns Horizontal Alignment of label
  211. * @returns {TEXT_ALIGNMENT_LEFT|TEXT_ALIGNMENT_CENTER|TEXT_ALIGNMENT_RIGHT}
  212. */
  213. getTextHorizontalAlignment: function () {
  214. return this._labelRenderer.getHorizontalAlignment();
  215. },
  216. /**
  217. * Sets Vertical Alignment of label
  218. * @param {cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM} alignment
  219. */
  220. setTextVerticalAlignment: function (alignment) {
  221. this._labelRenderer.setVerticalAlignment(alignment);
  222. this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
  223. this._labelRendererAdaptDirty = true;
  224. },
  225. /**
  226. * Gets text vertical alignment.
  227. * @returns {VERTICAL_TEXT_ALIGNMENT_TOP|VERTICAL_TEXT_ALIGNMENT_CENTER|VERTICAL_TEXT_ALIGNMENT_BOTTOM}
  228. */
  229. getTextVerticalAlignment: function () {
  230. return this._labelRenderer.getVerticalAlignment();
  231. },
  232. /**
  233. * Sets the touch scale enabled of label.
  234. * @param {Boolean} enable
  235. */
  236. setTouchScaleChangeEnabled: function (enable) {
  237. this._touchScaleChangeEnabled = enable;
  238. },
  239. /**
  240. * Gets the touch scale enabled of label.
  241. * @returns {Boolean}
  242. */
  243. isTouchScaleChangeEnabled: function () {
  244. return this._touchScaleChangeEnabled;
  245. },
  246. _onPressStateChangedToNormal: function () {
  247. if (!this._touchScaleChangeEnabled)
  248. return;
  249. this._labelRenderer.setScaleX(this._normalScaleValueX);
  250. this._labelRenderer.setScaleY(this._normalScaleValueY);
  251. },
  252. _onPressStateChangedToPressed: function () {
  253. if (!this._touchScaleChangeEnabled)
  254. return;
  255. this._labelRenderer.setScaleX(this._normalScaleValueX + this._onSelectedScaleOffset);
  256. this._labelRenderer.setScaleY(this._normalScaleValueY + this._onSelectedScaleOffset);
  257. },
  258. _onPressStateChangedToDisabled: function () {
  259. },
  260. _updateFlippedX: function () {
  261. if (this._flippedX)
  262. this._labelRenderer.setScaleX(-1.0);
  263. else
  264. this._labelRenderer.setScaleX(1.0);
  265. },
  266. _updateFlippedY: function () {
  267. if (this._flippedY)
  268. this._labelRenderer.setScaleY(-1.0);
  269. else
  270. this._labelRenderer.setScaleY(1.0);
  271. },
  272. _onSizeChanged: function () {
  273. ccui.Widget.prototype._onSizeChanged.call(this);
  274. this._labelRendererAdaptDirty = true;
  275. },
  276. _adaptRenderers: function(){
  277. if (this._labelRendererAdaptDirty) {
  278. this._labelScaleChangedWithSize();
  279. this._labelRendererAdaptDirty = false;
  280. }
  281. },
  282. /**
  283. * Returns the renderer's content size.
  284. * @override
  285. * @returns {cc.Size}
  286. */
  287. getVirtualRendererSize: function(){
  288. return this._labelRenderer.getContentSize();
  289. },
  290. /**
  291. * Returns the renderer of ccui.Text.
  292. * @returns {cc.Node}
  293. */
  294. getVirtualRenderer: function () {
  295. return this._labelRenderer;
  296. },
  297. _labelScaleChangedWithSize: function () {
  298. var locContentSize = this._contentSize;
  299. if (this._ignoreSize) {
  300. this._labelRenderer.setScale(1.0);
  301. this._normalScaleValueX = this._normalScaleValueY = 1;
  302. } else {
  303. this._labelRenderer.setDimensions(cc.size(locContentSize.width, locContentSize.height));
  304. var textureSize = this._labelRenderer.getContentSize();
  305. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  306. this._labelRenderer.setScale(1.0);
  307. return;
  308. }
  309. var scaleX = locContentSize.width / textureSize.width;
  310. var scaleY = locContentSize.height / textureSize.height;
  311. this._labelRenderer.setScaleX(scaleX);
  312. this._labelRenderer.setScaleY(scaleY);
  313. this._normalScaleValueX = scaleX;
  314. this._normalScaleValueY = scaleY;
  315. }
  316. this._labelRenderer.setPosition(locContentSize.width / 2.0, locContentSize.height / 2.0);
  317. },
  318. /**
  319. * Returns the "class name" of ccui.Text.
  320. * @returns {string}
  321. */
  322. getDescription: function () {
  323. return "Label";
  324. },
  325. /**
  326. * Enables shadow style and sets color, offset and blur radius styles.
  327. * @param {cc.Color} shadowColor
  328. * @param {cc.Size} offset
  329. * @param {Number} blurRadius
  330. */
  331. enableShadow: function(shadowColor, offset, blurRadius){
  332. this._labelRenderer.enableShadow(shadowColor, offset, blurRadius);
  333. },
  334. /**
  335. * Enables outline style and sets outline's color and size.
  336. * @param {cc.Color} outlineColor
  337. * @param {cc.Size} outlineSize
  338. */
  339. enableOutline: function(outlineColor, outlineSize){
  340. this._labelRenderer.enableOutline(outlineColor, outlineSize);
  341. },
  342. /**
  343. * Enables glow color
  344. * @param glowColor
  345. */
  346. enableGlow: function(glowColor){
  347. if (this._type == ccui.Text.Type.TTF)
  348. this._labelRenderer.enableGlow(glowColor);
  349. },
  350. /**
  351. * Disables renderer's effect.
  352. */
  353. disableEffect: function(){
  354. if(this._labelRenderer.disableEffect)
  355. this._labelRenderer.disableEffect();
  356. },
  357. _createCloneInstance: function () {
  358. return ccui.Text.create();
  359. },
  360. _copySpecialProperties: function (uiLabel) {
  361. if(uiLabel instanceof ccui.Text){
  362. this.setFontName(uiLabel._fontName);
  363. this.setFontSize(uiLabel.getFontSize());
  364. this.setString(uiLabel.getString());
  365. this.setTouchScaleChangeEnabled(uiLabel.touchScaleEnabled);
  366. this.setTextAreaSize(uiLabel._textAreaSize);
  367. this.setTextHorizontalAlignment(uiLabel._labelRenderer.getHorizontalAlignment());
  368. this.setTextVerticalAlignment(uiLabel._labelRenderer.getVerticalAlignment());
  369. }
  370. },
  371. _setBoundingWidth: function (value) {
  372. this._textAreaSize.width = value;
  373. this._labelRenderer._setBoundingWidth(value);
  374. this._labelScaleChangedWithSize();
  375. },
  376. _setBoundingHeight: function (value) {
  377. this._textAreaSize.height = value;
  378. this._labelRenderer._setBoundingHeight(value);
  379. this._labelScaleChangedWithSize();
  380. },
  381. _getBoundingWidth: function () {
  382. return this._textAreaSize.width;
  383. },
  384. _getBoundingHeight: function () {
  385. return this._textAreaSize.height;
  386. }
  387. });
  388. var _p = ccui.Text.prototype;
  389. // Extended properties
  390. /** @expose */
  391. _p.boundingWidth;
  392. cc.defineGetterSetter(_p, "boundingWidth", _p._getBoundingWidth, _p._setBoundingWidth);
  393. /** @expose */
  394. _p.boundingHeight;
  395. cc.defineGetterSetter(_p, "boundingHeight", _p._getBoundingHeight, _p._setBoundingHeight);
  396. /** @expose */
  397. _p.string;
  398. cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
  399. /** @expose */
  400. _p.stringLength;
  401. cc.defineGetterSetter(_p, "stringLength", _p.getStringLength);
  402. /** @expose */
  403. _p.font;
  404. cc.defineGetterSetter(_p, "font", _p._getFont, _p._setFont);
  405. /** @expose */
  406. _p.fontSize;
  407. cc.defineGetterSetter(_p, "fontSize", _p.getFontSize, _p.setFontSize);
  408. /** @expose */
  409. _p.fontName;
  410. cc.defineGetterSetter(_p, "fontName", _p.getFontName, _p.setFontName);
  411. /** @expose */
  412. _p.textAlign;
  413. cc.defineGetterSetter(_p, "textAlign", _p.getTextHorizontalAlignment, _p.setTextHorizontalAlignment);
  414. /** @expose */
  415. _p.verticalAlign;
  416. cc.defineGetterSetter(_p, "verticalAlign", _p.getTextVerticalAlignment, _p.setTextVerticalAlignment);
  417. _p = null;
  418. /**
  419. * allocates and initializes a UILabel.
  420. * @deprecated since v3.0, please use new ccui.Text() instead.
  421. * @return {ccui.Text}
  422. * @example
  423. * // example
  424. * var uiLabel = ccui.Text.create();
  425. */
  426. ccui.Label = ccui.Text.create = function (textContent, fontName, fontSize) {
  427. return new ccui.Text(textContent, fontName, fontSize);
  428. };
  429. /**
  430. * The zOrder value of ccui.Text's renderer.
  431. * @constant
  432. * @type {number}
  433. */
  434. ccui.Text.RENDERER_ZORDER = -1;
  435. /**
  436. * @ignore
  437. */
  438. ccui.Text.Type = {
  439. SYSTEM: 0,
  440. TTF: 1
  441. };