UITextField.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. * @ignore
  23. */
  24. //it's a private class, it's a renderer of ccui.TextField.
  25. ccui._TextFieldRenderer = cc.TextFieldTTF.extend({
  26. _maxLengthEnabled: false,
  27. _maxLength: 0,
  28. _passwordEnabled: false,
  29. _passwordStyleText: "",
  30. _attachWithIME: false,
  31. _detachWithIME: false,
  32. _insertText: false,
  33. _deleteBackward: false,
  34. _className: "_TextFieldRenderer",
  35. _textFieldRendererAdaptDirty: true,
  36. ctor: function () {
  37. cc.TextFieldTTF.prototype.ctor.call(this);
  38. this._maxLengthEnabled = false;
  39. this._maxLength = 0;
  40. this._passwordEnabled = false;
  41. this._passwordStyleText = "*";
  42. this._attachWithIME = false;
  43. this._detachWithIME = false;
  44. this._insertText = false;
  45. this._deleteBackward = false;
  46. },
  47. onEnter: function () {
  48. cc.TextFieldTTF.prototype.onEnter.call(this);
  49. cc.TextFieldTTF.prototype.setDelegate.call(this, this);
  50. },
  51. onTextFieldAttachWithIME: function (sender) {
  52. this.setAttachWithIME(true);
  53. return false;
  54. },
  55. onTextFieldInsertText: function (sender, text, len) {
  56. if (len == 1 && text == "\n")
  57. return false;
  58. this.setInsertText(true);
  59. return (this._maxLengthEnabled) && (cc.TextFieldTTF.prototype.getCharCount.call(this) >= this._maxLength);
  60. },
  61. onTextFieldDeleteBackward: function (sender, delText, nLen) {
  62. this.setDeleteBackward(true);
  63. return false;
  64. },
  65. onTextFieldDetachWithIME: function (sender) {
  66. this.setDetachWithIME(true);
  67. return false;
  68. },
  69. insertText: function (text, len) {
  70. var input_text = text;
  71. if (text != "\n"){
  72. if (this._maxLengthEnabled){
  73. var text_count = this.getString().length;
  74. if (text_count >= this._maxLength){
  75. // password
  76. if (this._passwordEnabled)
  77. this.setPasswordText(this.getString());
  78. return;
  79. }
  80. }
  81. }
  82. cc.TextFieldTTF.prototype.insertText.call(this, input_text, len);
  83. // password
  84. if (this._passwordEnabled && cc.TextFieldTTF.prototype.getCharCount.call(this) > 0)
  85. this.setPasswordText(this.getString());
  86. },
  87. deleteBackward: function () {
  88. cc.TextFieldTTF.prototype.deleteBackward.call(this);
  89. if (cc.TextFieldTTF.prototype.getCharCount.call(this) > 0 && this._passwordEnabled)
  90. this.setPasswordText(this._inputText);
  91. },
  92. openIME: function () {
  93. cc.TextFieldTTF.prototype.attachWithIME.call(this);
  94. },
  95. closeIME: function () {
  96. cc.TextFieldTTF.prototype.detachWithIME.call(this);
  97. },
  98. setMaxLengthEnabled: function (enable) {
  99. this._maxLengthEnabled = enable;
  100. },
  101. isMaxLengthEnabled: function () {
  102. return this._maxLengthEnabled;
  103. },
  104. setMaxLength: function (length) {
  105. this._maxLength = length;
  106. },
  107. getMaxLength: function () {
  108. return this._maxLength;
  109. },
  110. getCharCount: function () {
  111. return cc.TextFieldTTF.prototype.getCharCount.call(this);
  112. },
  113. setPasswordEnabled: function (enable) {
  114. this._passwordEnabled = enable;
  115. },
  116. isPasswordEnabled: function () {
  117. return this._passwordEnabled;
  118. },
  119. setPasswordStyleText: function (styleText) {
  120. if (styleText.length > 1)
  121. return;
  122. var header = styleText.charCodeAt(0);
  123. if (header < 33 || header > 126)
  124. return;
  125. this._passwordStyleText = styleText;
  126. },
  127. setPasswordText: function (text) {
  128. var tempStr = "";
  129. var text_count = text.length;
  130. var max = text_count;
  131. if (this._maxLengthEnabled && text_count > this._maxLength)
  132. max = this._maxLength;
  133. for (var i = 0; i < max; ++i)
  134. tempStr += this._passwordStyleText;
  135. cc.LabelTTF.prototype.setString.call(this, tempStr);
  136. },
  137. setAttachWithIME: function (attach) {
  138. this._attachWithIME = attach;
  139. },
  140. getAttachWithIME: function () {
  141. return this._attachWithIME;
  142. },
  143. setDetachWithIME: function (detach) {
  144. this._detachWithIME = detach;
  145. },
  146. getDetachWithIME: function () {
  147. return this._detachWithIME;
  148. },
  149. setInsertText: function (insert) {
  150. this._insertText = insert;
  151. },
  152. getInsertText: function () {
  153. return this._insertText;
  154. },
  155. setDeleteBackward: function (deleteBackward) {
  156. this._deleteBackward = deleteBackward;
  157. },
  158. getDeleteBackward: function () {
  159. return this._deleteBackward;
  160. },
  161. onDraw: function (sender) {
  162. return false;
  163. }
  164. });
  165. ccui._TextFieldRenderer.create = function (placeholder, fontName, fontSize) {
  166. var ret = new ccui._TextFieldRenderer();
  167. if (ret && ret.initWithString("", fontName, fontSize)) {
  168. if (placeholder)
  169. ret.setPlaceHolder(placeholder);
  170. return ret;
  171. }
  172. return null;
  173. };
  174. /**
  175. *
  176. * @class
  177. * @extends ccui.Widget
  178. *
  179. * @property {String} string - The content string of the label
  180. * @property {Number} placeHolder - The place holder of the text field
  181. * @property {String} font - The text field font with a style string: e.g. "18px Verdana"
  182. * @property {String} fontName - The text field font name
  183. * @property {Number} fontSize - The text field font size
  184. * @property {Boolean} maxLengthEnabled - Indicate whether max length limit is enabled
  185. * @property {Number} maxLength - The max length of the text field
  186. * @property {Boolean} passwordEnabled - Indicate whether the text field is for entering password
  187. */
  188. ccui.TextField = ccui.Widget.extend(/** @lends ccui.TextField# */{
  189. _textFieldRenderer: null,
  190. _touchWidth: 0,
  191. _touchHeight: 0,
  192. _useTouchArea: false,
  193. _textFieldEventListener: null,
  194. _textFieldEventSelector: null,
  195. _passwordStyleText: "",
  196. _textFieldRendererAdaptDirty: true,
  197. _fontName: "",
  198. _fontSize: 12,
  199. /**
  200. * allocates and initializes a UITextField.
  201. * Constructor of ccui.TextField. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  202. * @param {string} placeholder
  203. * @param {string} fontName
  204. * @param {Number} fontSize
  205. * @example
  206. * // example
  207. * var uiTextField = new ccui.TextField();
  208. */
  209. ctor: function (placeholder, fontName, fontSize) {
  210. ccui.Widget.prototype.ctor.call(this);
  211. if (placeholder)
  212. this.setPlaceHolder(placeholder);
  213. if (fontName)
  214. this.setFontName(fontName);
  215. if (fontSize)
  216. this.setFontSize(fontSize);
  217. },
  218. /**
  219. * Initializes a ccui.TextField. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
  220. * @returns {boolean}
  221. * @override
  222. */
  223. init: function(){
  224. if(ccui.Widget.prototype.init.call(this)){
  225. this.setTouchEnabled(true);
  226. return true;
  227. }
  228. return false;
  229. },
  230. /**
  231. * Calls parent class' onEnter and schedules update function.
  232. * @override
  233. */
  234. onEnter: function () {
  235. ccui.Widget.prototype.onEnter.call(this);
  236. this.scheduleUpdate();
  237. },
  238. _initRenderer: function () {
  239. this._textFieldRenderer = ccui._TextFieldRenderer.create("input words here", "Thonburi", 20);
  240. this.addProtectedChild(this._textFieldRenderer, ccui.TextField.RENDERER_ZORDER, -1);
  241. },
  242. /**
  243. * Sets touch size of ccui.TextField.
  244. * @param {cc.Size} size
  245. */
  246. setTouchSize: function (size) {
  247. this._touchWidth = size.width;
  248. this._touchHeight = size.height;
  249. },
  250. /**
  251. * Sets whether use touch area.
  252. * @param enable
  253. */
  254. setTouchAreaEnabled: function(enable){
  255. this._useTouchArea = enable;
  256. },
  257. /**
  258. * Checks a point if is in ccui.TextField's space
  259. * @param {cc.Point} pt
  260. * @returns {boolean}
  261. */
  262. hitTest: function(pt){
  263. if (this._useTouchArea) {
  264. var nsp = this.convertToNodeSpace(pt);
  265. var bb = cc.rect(
  266. -this._touchWidth * this._anchorPoint.x,
  267. -this._touchHeight * this._anchorPoint.y,
  268. this._touchWidth, this._touchHeight
  269. );
  270. return ( nsp.x >= bb.x && nsp.x <= bb.x + bb.width &&
  271. nsp.y >= bb.y && nsp.y <= bb.y + bb.height );
  272. } else
  273. return ccui.Widget.prototype.hitTest.call(this, pt);
  274. },
  275. /**
  276. * Returns touch size of ccui.TextField.
  277. * @returns {cc.Size}
  278. */
  279. getTouchSize: function () {
  280. return cc.size(this._touchWidth, this._touchHeight);
  281. },
  282. /**
  283. * Changes the string value of textField.
  284. * @deprecated since v3.0, please use setString instead.
  285. * @param {String} text
  286. */
  287. setText: function (text) {
  288. cc.log("Please use the setString");
  289. this.setString(text);
  290. },
  291. /**
  292. * Changes the string value of textField.
  293. * @param {String} text
  294. */
  295. setString: function (text) {
  296. if (text == null)
  297. return;
  298. text = String(text);
  299. if (this.isMaxLengthEnabled())
  300. text = text.substr(0, this.getMaxLength());
  301. if (this.isPasswordEnabled()) {
  302. this._textFieldRenderer.setPasswordText(text);
  303. this._textFieldRenderer.setString("");
  304. this._textFieldRenderer.insertText(text, text.length);
  305. } else
  306. this._textFieldRenderer.setString(text);
  307. this._textFieldRendererAdaptDirty = true;
  308. this._updateContentSizeWithTextureSize(this._textFieldRenderer.getContentSize());
  309. },
  310. /**
  311. * Sets the placeholder string. <br />
  312. * display this string if string equal "".
  313. * @param {String} value
  314. */
  315. setPlaceHolder: function (value) {
  316. this._textFieldRenderer.setPlaceHolder(value);
  317. this._textFieldRendererAdaptDirty = true;
  318. this._updateContentSizeWithTextureSize(this._textFieldRenderer.getContentSize());
  319. },
  320. /**
  321. * Returns the placeholder string.
  322. * @returns {String}
  323. */
  324. getPlaceHolder: function () {
  325. return this._textFieldRenderer.getPlaceHolder();
  326. },
  327. /**
  328. * Returns the color of ccui.TextField's place holder.
  329. * @returns {cc.Color}
  330. */
  331. getPlaceHolderColor: function(){
  332. return this._textFieldRenderer.getPlaceHolderColor();
  333. },
  334. /**
  335. * Sets the place holder color to ccui.TextField.
  336. * @param color
  337. */
  338. setPlaceHolderColor: function(color){
  339. this._textFieldRenderer.setColorSpaceHolder(color);
  340. },
  341. /**
  342. * Sets the text color to ccui.TextField
  343. * @param textColor
  344. */
  345. setTextColor: function(textColor){
  346. this._textFieldRenderer.setTextColor(textColor);
  347. },
  348. /**
  349. * Sets font size for ccui.TextField.
  350. * @param {Number} size
  351. */
  352. setFontSize: function (size) {
  353. this._textFieldRenderer.setFontSize(size);
  354. this._fontSize = size;
  355. this._textFieldRendererAdaptDirty = true;
  356. this._updateContentSizeWithTextureSize(this._textFieldRenderer.getContentSize());
  357. },
  358. /**
  359. * Gets font size of ccui.TextField.
  360. * @return {Number} size
  361. */
  362. getFontSize: function () {
  363. return this._fontSize;
  364. },
  365. /**
  366. * Sets font name for ccui.TextField
  367. * @param {String} name
  368. */
  369. setFontName: function (name) {
  370. this._textFieldRenderer.setFontName(name);
  371. this._fontName = name;
  372. this._textFieldRendererAdaptDirty = true;
  373. this._updateContentSizeWithTextureSize(this._textFieldRenderer.getContentSize());
  374. },
  375. /**
  376. * Returns font name of ccui.TextField.
  377. * @return {String} font name
  378. */
  379. getFontName: function () {
  380. return this._fontName;
  381. },
  382. /**
  383. * detach with IME
  384. */
  385. didNotSelectSelf: function () {
  386. this._textFieldRenderer.detachWithIME();
  387. },
  388. /**
  389. * Returns textField string value
  390. * @deprecated since v3.0, please use getString instead.
  391. * @returns {String}
  392. */
  393. getStringValue: function () {
  394. cc.log("Please use the getString");
  395. return this.getString();
  396. },
  397. /**
  398. * Returns string value of ccui.TextField.
  399. * @returns {String}
  400. */
  401. getString: function () {
  402. return this._textFieldRenderer.getString();
  403. },
  404. /**
  405. * Returns the length of ccui.TextField.
  406. * @returns {Number}
  407. */
  408. getStringLength: function(){
  409. return this._textFieldRenderer.getStringLength();
  410. },
  411. /**
  412. * The touch began event callback handler.
  413. * @param {cc.Point} touchPoint
  414. */
  415. onTouchBegan: function (touchPoint, unusedEvent) {
  416. var self = this;
  417. var pass = ccui.Widget.prototype.onTouchBegan.call(self, touchPoint, unusedEvent);
  418. if (self._hit) {
  419. setTimeout(function(){
  420. self._textFieldRenderer.attachWithIME();
  421. }, 0);
  422. }
  423. return pass;
  424. },
  425. /**
  426. * Sets Whether to open string length limit for ccui.TextField.
  427. * @param {Boolean} enable
  428. */
  429. setMaxLengthEnabled: function (enable) {
  430. this._textFieldRenderer.setMaxLengthEnabled(enable);
  431. },
  432. /**
  433. * Returns Whether to open string length limit.
  434. * @returns {Boolean}
  435. */
  436. isMaxLengthEnabled: function () {
  437. return this._textFieldRenderer.isMaxLengthEnabled();
  438. },
  439. /**
  440. * Sets the max length of ccui.TextField. Only when you turn on the string length limit, it is valid.
  441. * @param {number} length
  442. */
  443. setMaxLength: function (length) {
  444. this._textFieldRenderer.setMaxLength(length);
  445. this.setString(this.getString());
  446. },
  447. /**
  448. * Returns the max length of ccui.TextField.
  449. * @returns {number} length
  450. */
  451. getMaxLength: function () {
  452. return this._textFieldRenderer.getMaxLength();
  453. },
  454. /**
  455. * Sets whether to open setting string as password character.
  456. * @param {Boolean} enable
  457. */
  458. setPasswordEnabled: function (enable) {
  459. this._textFieldRenderer.setPasswordEnabled(enable);
  460. },
  461. /**
  462. * Returns whether to open setting string as password character.
  463. * @returns {Boolean}
  464. */
  465. isPasswordEnabled: function () {
  466. return this._textFieldRenderer.isPasswordEnabled();
  467. },
  468. /**
  469. * Sets the password style character, Only when you turn on setting string as password character, it is valid.
  470. * @param styleText
  471. */
  472. setPasswordStyleText: function(styleText){
  473. this._textFieldRenderer.setPasswordStyleText(styleText);
  474. this._passwordStyleText = styleText;
  475. this.setString(this.getString());
  476. },
  477. /**
  478. * Returns the password style character.
  479. * @returns {String}
  480. */
  481. getPasswordStyleText: function () {
  482. return this._passwordStyleText;
  483. },
  484. update: function (dt) {
  485. if (this.getAttachWithIME()) {
  486. this._attachWithIMEEvent();
  487. this.setAttachWithIME(false);
  488. }
  489. if (this.getDetachWithIME()) {
  490. this._detachWithIMEEvent();
  491. this.setDetachWithIME(false);
  492. }
  493. if (this.getInsertText()) {
  494. this._insertTextEvent();
  495. this.setInsertText(false);
  496. this._textFieldRendererAdaptDirty = true;
  497. this._updateContentSizeWithTextureSize(this._textFieldRenderer.getContentSize());
  498. }
  499. if (this.getDeleteBackward()) {
  500. this._deleteBackwardEvent();
  501. this.setDeleteBackward(false);
  502. this._textFieldRendererAdaptDirty = true;
  503. this._updateContentSizeWithTextureSize(this._textFieldRenderer.getContentSize());
  504. }
  505. },
  506. /**
  507. * Returns whether attach with IME.
  508. * @returns {Boolean}
  509. */
  510. getAttachWithIME: function () {
  511. return this._textFieldRenderer.getAttachWithIME();
  512. },
  513. /**
  514. * Sets attach with IME.
  515. * @param {Boolean} attach
  516. */
  517. setAttachWithIME: function (attach) {
  518. this._textFieldRenderer.setAttachWithIME(attach);
  519. },
  520. /**
  521. * Returns whether detach with IME.
  522. * @returns {Boolean}
  523. */
  524. getDetachWithIME: function () {
  525. return this._textFieldRenderer.getDetachWithIME();
  526. },
  527. /**
  528. * Sets detach with IME.
  529. * @param {Boolean} detach
  530. */
  531. setDetachWithIME: function (detach) {
  532. this._textFieldRenderer.setDetachWithIME(detach);
  533. },
  534. /**
  535. * Returns insertText string of ccui.TextField.
  536. * @returns {String}
  537. */
  538. getInsertText: function () {
  539. return this._textFieldRenderer.getInsertText();
  540. },
  541. /**
  542. * Sets insertText string to ccui.TextField.
  543. * @param {String} insertText
  544. */
  545. setInsertText: function (insertText) {
  546. this._textFieldRenderer.setInsertText(insertText);
  547. },
  548. /**
  549. * Returns the delete backward of ccui.TextField.
  550. * @returns {Boolean}
  551. */
  552. getDeleteBackward: function () {
  553. return this._textFieldRenderer.getDeleteBackward();
  554. },
  555. /**
  556. * Sets the delete backward of ccui.TextField.
  557. * @param {Boolean} deleteBackward
  558. */
  559. setDeleteBackward: function (deleteBackward) {
  560. this._textFieldRenderer.setDeleteBackward(deleteBackward);
  561. },
  562. _attachWithIMEEvent: function () {
  563. if(this._textFieldEventSelector){
  564. if (this._textFieldEventListener)
  565. this._textFieldEventSelector.call(this._textFieldEventListener, this, ccui.TextField.EVENT_ATTACH_WITH_IME);
  566. else
  567. this._textFieldEventSelector(this, ccui.TextField.EVENT_ATTACH_WITH_IME);
  568. }
  569. },
  570. _detachWithIMEEvent: function () {
  571. if(this._textFieldEventSelector){
  572. if (this._textFieldEventListener)
  573. this._textFieldEventSelector.call(this._textFieldEventListener, this, ccui.TextField.EVENT_DETACH_WITH_IME);
  574. else
  575. this._textFieldEventSelector(this, ccui.TextField.EVENT_DETACH_WITH_IME);
  576. }
  577. },
  578. _insertTextEvent: function () {
  579. if(this._textFieldEventSelector){
  580. if (this._textFieldEventListener)
  581. this._textFieldEventSelector.call(this._textFieldEventListener, this, ccui.TextField.EVENT_INSERT_TEXT);
  582. else
  583. this._textFieldEventSelector(this, ccui.TextField.EVENT_INSERT_TEXT);
  584. }
  585. },
  586. _deleteBackwardEvent: function () {
  587. if(this._textFieldEventSelector){
  588. if (this._textFieldEventListener)
  589. this._textFieldEventSelector.call(this._textFieldEventListener, this, ccui.TextField.EVENT_DELETE_BACKWARD);
  590. else
  591. this._textFieldEventSelector(this, ccui.TextField.EVENT_DELETE_BACKWARD);
  592. }
  593. },
  594. /**
  595. * Adds event listener to cuci.TextField.
  596. * @param {Object} [target=]
  597. * @param {Function} selector
  598. * @deprecated since v3.0, please use addEventListener instead.
  599. */
  600. addEventListenerTextField: function (selector, target) {
  601. this.addEventListener(selector, target);
  602. },
  603. /**
  604. * Adds event listener callback.
  605. * @param {Object} [target=]
  606. * @param {Function} selector
  607. */
  608. addEventListener: function(selector, target){
  609. this._textFieldEventSelector = selector;
  610. this._textFieldEventListener = target;
  611. },
  612. _onSizeChanged: function () {
  613. ccui.Widget.prototype._onSizeChanged.call(this);
  614. this._textFieldRendererAdaptDirty = true;
  615. },
  616. _adaptRenderers: function(){
  617. if (this._textFieldRendererAdaptDirty) {
  618. this._textfieldRendererScaleChangedWithSize();
  619. this._textFieldRendererAdaptDirty = false;
  620. }
  621. },
  622. _textfieldRendererScaleChangedWithSize: function () {
  623. if (!this._ignoreSize)
  624. this._textFieldRenderer.setDimensions(this._contentSize);
  625. this._textFieldRenderer.setPosition(this._contentSize.width / 2, this._contentSize.height / 2);
  626. },
  627. /**
  628. * Returns the ccui.TextField's content size.
  629. * @returns {cc.Size}
  630. */
  631. getVirtualRendererSize: function(){
  632. return this._textFieldRenderer.getContentSize();
  633. },
  634. /**
  635. * Returns the renderer of ccui.TextField.
  636. * @returns {cc.Node}
  637. */
  638. getVirtualRenderer: function () {
  639. return this._textFieldRenderer;
  640. },
  641. /**
  642. * Returns the "class name" of ccui.TextField.
  643. * @returns {string}
  644. */
  645. getDescription: function () {
  646. return "TextField";
  647. },
  648. /**
  649. * Open keyboard and receive input text.
  650. * @return {Boolean}
  651. */
  652. attachWithIME: function () {
  653. this._textFieldRenderer.attachWithIME();
  654. },
  655. _createCloneInstance: function () {
  656. return ccui.TextField.create();
  657. },
  658. _copySpecialProperties: function (textField) {
  659. this.setString(textField._textFieldRenderer.getString());
  660. this.setPlaceHolder(textField.getString());
  661. this.setFontSize(textField._textFieldRenderer.getFontSize());
  662. this.setFontName(textField._textFieldRenderer.getFontName());
  663. this.setMaxLengthEnabled(textField.isMaxLengthEnabled());
  664. this.setMaxLength(textField.getMaxLength());
  665. this.setPasswordEnabled(textField.isPasswordEnabled());
  666. this.setPasswordStyleText(textField._passwordStyleText);
  667. this.setAttachWithIME(textField.getAttachWithIME());
  668. this.setDetachWithIME(textField.getDetachWithIME());
  669. this.setInsertText(textField.getInsertText());
  670. this.setDeleteBackward(textField.getDeleteBackward());
  671. },
  672. /**
  673. * Sets the text area size to ccui.TextField.
  674. * @param {cc.Size} size
  675. */
  676. setTextAreaSize: function(size){
  677. this.setContentSize(size);
  678. },
  679. /**
  680. * Sets the text horizontal alignment of ccui.TextField.
  681. * @param alignment
  682. */
  683. setTextHorizontalAlignment: function(alignment){
  684. this._textFieldRenderer.setHorizontalAlignment(alignment);
  685. },
  686. /**
  687. * Sets the text vertical alignment of ccui.TextField.
  688. * @param alignment
  689. */
  690. setTextVerticalAlignment: function(alignment){
  691. this._textFieldRenderer.setVerticalAlignment(alignment);
  692. },
  693. _setFont: function (font) {
  694. this._textFieldRenderer._setFont(font);
  695. this._textFieldRendererAdaptDirty = true;
  696. },
  697. _getFont: function () {
  698. return this._textFieldRenderer._getFont();
  699. }
  700. });
  701. /**
  702. * Creates a ccui.TextField.
  703. * @deprecated since v3.0, please use new ccui.TextField() instead.
  704. * @param {String} placeholder
  705. * @param {String} fontName
  706. * @param {Number} fontSize
  707. * @returns {ccui.TextField}
  708. * @example
  709. * // example
  710. * var uiTextField = ccui.TextField.create();
  711. */
  712. ccui.TextField.create = function(placeholder, fontName, fontSize){
  713. return new ccui.TextField(placeholder, fontName, fontSize);
  714. };
  715. var _p = ccui.TextField.prototype;
  716. // Extended properties
  717. /** @expose */
  718. _p.string;
  719. cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
  720. /** @expose */
  721. _p.placeHolder;
  722. cc.defineGetterSetter(_p, "placeHolder", _p.getPlaceHolder, _p.setPlaceHolder);
  723. /** @expose */
  724. _p.font;
  725. cc.defineGetterSetter(_p, "font", _p._getFont, _p._setFont);
  726. /** @expose */
  727. _p.fontSize;
  728. cc.defineGetterSetter(_p, "fontSize", _p.getFontSize, _p.setFontSize);
  729. /** @expose */
  730. _p.fontName;
  731. cc.defineGetterSetter(_p, "fontName", _p.getFontName, _p.setFontName);
  732. /** @expose */
  733. _p.maxLengthEnabled;
  734. cc.defineGetterSetter(_p, "maxLengthEnabled", _p.isMaxLengthEnabled, _p.setMaxLengthEnabled);
  735. /** @expose */
  736. _p.maxLength;
  737. cc.defineGetterSetter(_p, "maxLength", _p.getMaxLength, _p.setMaxLength);
  738. /** @expose */
  739. _p.passwordEnabled;
  740. cc.defineGetterSetter(_p, "passwordEnabled", _p.isPasswordEnabled, _p.setPasswordEnabled);
  741. _p = null;
  742. // Constants
  743. //TextField event
  744. /**
  745. * The attach with IME event flag of ccui.TextField
  746. * @constant
  747. * @type {number}
  748. */
  749. ccui.TextField.EVENT_ATTACH_WITH_IME = 0;
  750. /**
  751. * The detach with IME event flag of ccui.TextField
  752. * @constant
  753. * @type {number}
  754. */
  755. ccui.TextField.EVENT_DETACH_WITH_IME = 1;
  756. /**
  757. * The insert text event flag of ccui.TextField
  758. * @constant
  759. * @type {number}
  760. */
  761. ccui.TextField.EVENT_INSERT_TEXT = 2;
  762. /**
  763. * The delete backward event flag of ccui.TextField
  764. * @constant
  765. * @type {number}
  766. */
  767. ccui.TextField.EVENT_DELETE_BACKWARD = 3;
  768. /**
  769. * The zOrder value of ccui.TextField's renderer.
  770. * @constant
  771. * @type {number}
  772. */
  773. ccui.TextField.RENDERER_ZORDER = -1;