From 5bf14a8f4c39b474ed03e64bd8c17d0fe302eba6 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 26 Feb 2016 10:28:49 +0800 Subject: [PATCH 1/3] Improve EditBox change SpriteFrame to Sprite rename placeHolder to placeholder fix placeholder Revert "change SpriteFrame to Sprite" This reverts commit d968d8ac9a01d91b3055830aab109b934ae27138. fix clear spriteframe fix init size and label font size add line height support --- cocos2d/core/components/CCEditBox.js | 82 +++++++++++++++++++++------- cocos2d/core/editbox/CCSGEditBox.js | 56 +++++++++++++------ cocos2d/core/editbox/CCdomNode.js | 2 +- jsb/jsb-editbox.js | 12 ++-- 4 files changed, 107 insertions(+), 45 deletions(-) diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index a2095c6991..1ba9a6eae0 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -44,6 +44,7 @@ var EditBox = cc.Class({ }, properties: { + _useOriginalSize: true, _string: '', /** * Input string of EditBox. @@ -68,8 +69,14 @@ var EditBox = cc.Class({ notify: function() { var sgNode = this._sgNode; var backgroundSprite = sgNode.getBackgroundSprite(); - backgroundSprite.setSpriteFrame(this.backgroundImage); - backgroundSprite.setContentSize(sgNode.getContentSize()); + if(this.backgroundImage) { + var sprite = this._createBackgroundSprite(); + sprite.setSpriteFrame(this.backgroundImage); + sprite.setContentSize(sgNode.getContentSize()); + } + else { + backgroundSprite.removeFromParent(); + } } }, @@ -125,6 +132,17 @@ var EditBox = cc.Class({ } }, + /** + * Change the lineHeight of displayed text. + * @property {Number} lineHeight + */ + lineHeight: { + default: 40, + notify: function() { + this._sgNode.setLineHeight(this.lineHeight); + } + }, + /** * Font color of the input text. * @property {cc.Color} fontColor @@ -137,35 +155,35 @@ var EditBox = cc.Class({ }, /** - * The display text of placeHolder. - * @property {String} placeHolder + * The display text of placeholder. + * @property {String} placeholder */ - placeHolder: { + placeholder: { default: 'Enter text here...', notify: function() { - this._sgNode.placeHolder = this.placeHolder; + this._sgNode.placeholder = this.placeholder; } }, /** - * The font size of placeHolder. - * @property {Number} placeHolderFontSize + * The font size of placeholder. + * @property {Number} placeholderFontSize */ - placeHolderFontSize: { + placeholderFontSize: { default: 20, notify: function() { - this._sgNode.placeHolderFontSize = this.placeHolderFontSize; + this._sgNode.placeholderFontSize = this.placeholderFontSize; } }, /** - * The font color of placeHolder. - * @property {cc.Color} placeHolderFontColor + * The font color of placeholder. + * @property {cc.Color} placeholderFontColor */ - placeHolderFontColor: { + placeholderFontColor: { default: cc.Color.GRAY, notify: function() { - this._sgNode.placeHolderFontColor = this.placeHolderFontColor; + this._sgNode.placeholderFontColor = this.placeholderFontColor; } }, @@ -216,15 +234,21 @@ var EditBox = cc.Class({ }, _createSgNode: function() { - return new _ccsg.EditBox(cc.size(300, 100)); + return new _ccsg.EditBox(cc.size(160, 40)); + }, + + _createBackgroundSprite: function() { + var sgNode = this._sgNode; + var bgSprite = new cc.Scale9Sprite(this.backgroundImage); + sgNode.initWithSizeAndBackgroundSprite(this.node.getContentSize(), bgSprite); + return bgSprite; }, _initSgNode: function() { var sgNode = this._sgNode; - var bgSprite = new cc.Scale9Sprite(this.backgroundImage); - sgNode.initWithSizeAndBackgroundSprite(this.node.getContentSize(), bgSprite); - sgNode.setContentSize(this.node.getContentSize()); + this._createBackgroundSprite(); + sgNode.inputMode = this.inputMode; sgNode.maxLength = this.maxLength; @@ -232,15 +256,31 @@ var EditBox = cc.Class({ sgNode.string = this._string; sgNode.fontSize = this.fontSize; sgNode.fontColor = this.fontColor; - sgNode.placeHolder = this.placeHolder; - sgNode.placeHolderFontSize = this.placeHolderFontSize; - sgNode.placeHolderFontColor = this.placeHolderFontColor; + sgNode.placeholder = this.placeholder; + sgNode.placeholderFontSize = this.placeholderFontSize; + sgNode.placeholderFontColor = this.placeholderFontColor; sgNode.inputFlag = this.inputFlag; sgNode.returnType = this.returnType; + sgNode.setLineHeight(this.lineHeight); + + if (!this._useOriginalSize) { + sgNode.setContentSize(this.node.getContentSize()); + } sgNode.setDelegate(this); }, + _resized: function () { + this._useOriginalSize = false; + }, + + onLoad: function () { + this._super(); + + this.node.on('size-changed', this._resized, this); + this.node.setContentSize(this._sgNode.getContentSize()); + }, + editBoxEditingDidBegan: function() { cc.Component.EventHandler.emitEvents(this.editingDidBegan); }, diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 6c437677fc..6ee3586489 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -26,6 +26,13 @@ THE SOFTWARE. ****************************************************************************/ +var capitalize = function(string) { + return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); }); +}; + +function capitalizeFirstLetter(string) { + return string.charAt(0).toUpperCase() + string.slice(1); +} /** * Enum for keyboard return types * @readonly @@ -238,17 +245,17 @@ EditBoxImpl.prototype = { this._placeholderLabel.setColor(cc.Color.GRAY); this._editBox.addChild(this._placeholderLabel, 100); - this._updateLabelSize(editBoxSize); }, _updateLabelSize: function (editBoxSize) { this._textLabel.setPosition(cc.p(0, editBoxSize.height)); - this._textLabel.setFontSize(editBoxSize.height * 2.0 /3.0); - this._textLabel.setLineHeight(this._textLabel.getContentSize().height); - this._placeholderLabel.setFontSize(editBoxSize.height * 2 / 3.0); - this._placeholderLabel.setLineHeight(this._placeholderLabel.getContentSize().height); this._placeholderLabel.setPosition(cc.p(0, editBoxSize.height)); + this._placeholderLabel.setLineHeight(editBoxSize.height); + }, + + setLineHeight: function (lineHeight) { + this._textLabel.setLineHeight(lineHeight); }, _hiddenLabels: function () { @@ -259,6 +266,7 @@ EditBoxImpl.prototype = { _updateLabelString: function() { this._textLabel.setVisible(true); this._textLabel.setString(this._editBox._text); + var inputFlag = this._editBox._editBoxInputFlag; if (this._edTxt.type === 'password') { var passwordString = ''; var len = this._editBox._text.length; @@ -267,6 +275,15 @@ EditBoxImpl.prototype = { } this._textLabel.setString(passwordString); } + else if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) { + this._textLabel.setString(this._editBox._text.toUpperCase()); + } + else if (inputFlag === InputFlag.INITIAL_CAPS_WORD) { + this._textLabel.setString(capitalize(this._editBox._text)); + } + else if (inputFlag === InputFlag.INITIAL_CAPS_SENTENCE) { + this._textLabel.setString(capitalizeFirstLetter(this._editBox._text)); + } }, _showLabels: function () { @@ -365,14 +382,12 @@ EditBoxImpl.prototype = { else this._edTxt.type = 'text'; - if (inputFlag === InputFlag.SENSITIVE || inputFlag === InputFlag.PASSWORD) { - this._edTxt.style.textTransform = 'none'; - } - else if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) { + this._edTxt.style.textTransform = 'none'; + + if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) { this._edTxt.style.textTransform = 'uppercase'; } - else if (inputFlag === InputFlag.INITIAL_CAPS_SENTENCE - || inputFlag === InputFlag.INITIAL_CAPS_WORD) { + else if (inputFlag === InputFlag.INITIAL_CAPS_WORD) { this._edTxt.style.textTransform = 'capitalize'; } this._updateLabelString(); @@ -695,8 +710,10 @@ _ccsg.EditBox = _ccsg.Node.extend({ this._updateBackgroundSpriteSizeAndPosition(newWidth, newHeight); this._nativeControl.setSize(newWidth, newHeight); + }, - this.setFontSize(height * 2 / 3); + setLineHeight: function (lineHeight) { + this._nativeControl.setLineHeight(lineHeight); }, /** @@ -874,9 +891,12 @@ _ccsg.EditBox = _ccsg.Node.extend({ * @param {cc.Color | cc.Scale9Sprite} normal9SpriteBg */ initWithSizeAndBackgroundSprite: function (size, normal9SpriteBg) { + if(this._backgroundSprite) { + this._backgroundSprite.removeFromParent(); + } this._backgroundSprite = normal9SpriteBg; - if(this._backgroundSprite) { + if(this._backgroundSprite && !this._backgroundSprite.parent) { this.addChild(this._backgroundSprite); this._updateBackgroundSpriteSizeAndPosition(size.width, size.height); @@ -982,19 +1002,19 @@ _p.maxLength; cc.defineGetterSetter(_p, 'maxLength', _p.getMaxLength, _p.setMaxLength); /** @expose */ _p.placeHolder; -cc.defineGetterSetter(_p, 'placeHolder', _p.getPlaceHolder, _p.setPlaceHolder); +cc.defineGetterSetter(_p, 'placeholder', _p.getPlaceHolder, _p.setPlaceHolder); /** @expose */ _p.placeHolderFont; -cc.defineGetterSetter(_p, 'placeHolderFont', null, _p._setPlaceholderFont); +cc.defineGetterSetter(_p, 'placeholderFont', null, _p._setPlaceholderFont); /** @expose */ _p.placeHolderFontName; -cc.defineGetterSetter(_p, 'placeHolderFontName', null, _p.setPlaceholderFontName); +cc.defineGetterSetter(_p, 'placeholderFontName', null, _p.setPlaceholderFontName); /** @expose */ _p.placeHolderFontSize; -cc.defineGetterSetter(_p, 'placeHolderFontSize', null, _p.setPlaceholderFontSize); +cc.defineGetterSetter(_p, 'placeholderFontSize', null, _p.setPlaceholderFontSize); /** @expose */ _p.placeHolderFontColor; -cc.defineGetterSetter(_p, 'placeHolderFontColor', null, _p.setPlaceholderFontColor); +cc.defineGetterSetter(_p, 'placeholderFontColor', null, _p.setPlaceholderFontColor); /** @expose */ _p.inputFlag; cc.defineGetterSetter(_p, 'inputFlag', null, _p.setInputFlag); diff --git a/cocos2d/core/editbox/CCdomNode.js b/cocos2d/core/editbox/CCdomNode.js index 5c9e0b065e..d772073465 100644 --- a/cocos2d/core/editbox/CCdomNode.js +++ b/cocos2d/core/editbox/CCdomNode.js @@ -603,7 +603,7 @@ cc.DOM.forSprite = function (x) { cc.DOM.placeHolder = function (x) { //creating a placeholder dom to simulate other ccNode in the hierachy x.dom = cc.$new('div'); - x.placeholder = true; + // x.placeholder = true; x.dom.style.position = 'absolute'; x.dom.style.bottom = 0; //x.dom.style.display='block'; diff --git a/jsb/jsb-editbox.js b/jsb/jsb-editbox.js index b90e8e604d..0145ce7ee0 100644 --- a/jsb/jsb-editbox.js +++ b/jsb/jsb-editbox.js @@ -31,11 +31,11 @@ cc.defineGetterSetter(_p, 'fontSize', null, _p.setFontSize); cc.defineGetterSetter(_p, 'fontColor', null, _p.setFontColor); cc.defineGetterSetter(_p, 'string', _p.getString, _p.setString); cc.defineGetterSetter(_p, 'maxLength', _p.getMaxLength, _p.setMaxLength); -cc.defineGetterSetter(_p, 'placeHolder', _p.getPlaceHolder, _p.setPlaceHolder); -cc.defineGetterSetter(_p, 'placeHolderFont', null, _p.setPlaceholderFont); -cc.defineGetterSetter(_p, 'placeHolderFontName', null, _p.setPlaceholderFontName); -cc.defineGetterSetter(_p, 'placeHolderFontSize', null, _p.setPlaceholderFontSize); -cc.defineGetterSetter(_p, 'placeHolderFontColor', null, _p.setPlaceholderFontColor); +cc.defineGetterSetter(_p, 'placeholder', _p.getPlaceHolder, _p.setPlaceHolder); +cc.defineGetterSetter(_p, 'placeholderFont', null, _p.setPlaceholderFont); +cc.defineGetterSetter(_p, 'placeholderFontName', null, _p.setPlaceholderFontName); +cc.defineGetterSetter(_p, 'placeholderFontSize', null, _p.setPlaceholderFontSize); +cc.defineGetterSetter(_p, 'placeholderFontColor', null, _p.setPlaceholderFontColor); cc.defineGetterSetter(_p, 'inputFlag', null, _p.setInputFlag); cc.defineGetterSetter(_p, 'delegate', null, _p.setDelegate); cc.defineGetterSetter(_p, 'inputMode', null, _p.setInputMode); @@ -49,4 +49,6 @@ _p.editBoxEditingDidEnd = function (sender) { this.editBoxEditingDidEnded(sender); } +_p.setLineHeight = function () {} + _p = null; From 532916cd437d8e5dc17eddda7a3c4d6ca14fcac4 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 26 Feb 2016 16:27:41 +0800 Subject: [PATCH 2/3] add i18n --- cocos2d/core/components/CCEditBox.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index 1ba9a6eae0..a08340ff32 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -51,6 +51,7 @@ var EditBox = cc.Class({ * @property {String} string */ string: { + tooltip: 'i18n:COMPONENT.editbox.string', get: function () { return this._sgNode.string; }, @@ -64,6 +65,7 @@ var EditBox = cc.Class({ * @property {cc.SpriteFrame} backGroundImage */ backgroundImage: { + tooltip: 'i18n:COMPONENT.editbox.backgroundImage', default: null, type: cc.SpriteFrame, notify: function() { @@ -88,6 +90,8 @@ var EditBox = cc.Class({ */ returnType: { default: KeyboardReturnType.DEFAULT, + tooltip: 'i18n:COMPONENT.editbox.returnType', + displayName: 'KeyboardReturnType', type: KeyboardReturnType, notify: function() { this._sgNode.returnType = this.returnType; @@ -100,13 +104,13 @@ var EditBox = cc.Class({ * @default InputFlag.INITIAL_CAPS_ALL_CHARACTERS */ inputFlag: { + tooltip: 'i18n:COMPONENT.editbox.input_flag', default: InputFlag.INITIAL_CAPS_ALL_CHARACTERS, type: InputFlag, notify: function() { this._sgNode.inputFlag = this.inputFlag; } }, - /** * Set the input mode of the edit box. * If you pass ANY, it will create a multiline EditBox. @@ -114,6 +118,7 @@ var EditBox = cc.Class({ * @default InputMode.ANY */ inputMode: { + tooltip: 'i18n:COMPONENT.editbox.input_mode', default: InputMode.ANY, type: InputMode, notify: function() { @@ -126,6 +131,7 @@ var EditBox = cc.Class({ * @property {Number} fontSize */ fontSize: { + tooltip: 'i18n:COMPONENT.editbox.font_size', default: 20, notify: function() { this._sgNode.fontSize = this.fontSize; @@ -137,6 +143,7 @@ var EditBox = cc.Class({ * @property {Number} lineHeight */ lineHeight: { + tooltip: 'i18n:COMPONENT.editbox.line_height', default: 40, notify: function() { this._sgNode.setLineHeight(this.lineHeight); @@ -148,6 +155,7 @@ var EditBox = cc.Class({ * @property {cc.Color} fontColor */ fontColor: { + tooltip: 'i18n:COMPONENT.editbox.font_color', default: cc.Color.WHITE, notify: function() { this._sgNode.fontColor = this.fontColor; @@ -159,6 +167,7 @@ var EditBox = cc.Class({ * @property {String} placeholder */ placeholder: { + tooltip: 'i18n:COMPONENT.editbox.placeholder', default: 'Enter text here...', notify: function() { this._sgNode.placeholder = this.placeholder; @@ -170,6 +179,7 @@ var EditBox = cc.Class({ * @property {Number} placeholderFontSize */ placeholderFontSize: { + tooltip: 'i18n:COMPONENT.editbox.placeholder_font_size', default: 20, notify: function() { this._sgNode.placeholderFontSize = this.placeholderFontSize; @@ -181,6 +191,7 @@ var EditBox = cc.Class({ * @property {cc.Color} placeholderFontColor */ placeholderFontColor: { + tooltip: 'i18n:COMPONENT.editbox.placeholder_font_color', default: cc.Color.GRAY, notify: function() { this._sgNode.placeholderFontColor = this.placeholderFontColor; @@ -192,6 +203,7 @@ var EditBox = cc.Class({ * @property {Number} maxLength */ maxLength: { + tooltip: 'i18n:COMPONENT.editbox.max_length', default: 20, notify: function() { this._sgNode.maxLength = this.maxLength; From f9c747d0a9e39d5c891a70983444a31cd47a89b1 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 26 Feb 2016 18:13:29 +0800 Subject: [PATCH 3/3] cleanup code --- cocos2d/core/components/CCEditBox.js | 3 +-- cocos2d/core/editbox/CCSGEditBox.js | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index a08340ff32..2840ab1ba8 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -252,7 +252,7 @@ var EditBox = cc.Class({ _createBackgroundSprite: function() { var sgNode = this._sgNode; var bgSprite = new cc.Scale9Sprite(this.backgroundImage); - sgNode.initWithSizeAndBackgroundSprite(this.node.getContentSize(), bgSprite); + sgNode.initWithSizeAndBackgroundSprite(cc.size(160, 40), bgSprite); return bgSprite; }, @@ -290,7 +290,6 @@ var EditBox = cc.Class({ this._super(); this.node.on('size-changed', this._resized, this); - this.node.setContentSize(this._sgNode.getContentSize()); }, editBoxEditingDidBegan: function() { diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 6ee3586489..9e01af7162 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -235,7 +235,7 @@ EditBoxImpl.prototype = { _createLabels: function () { var editBoxSize = this._editBox.getContentSize(); - this._textLabel = new _ccsg.Label() + this._textLabel = new _ccsg.Label(); this._textLabel.setVisible(false); this._textLabel.setAnchorPoint(cc.p(0, 1)); this._editBox.addChild(this._textLabel, 100); @@ -244,7 +244,7 @@ EditBoxImpl.prototype = { this._placeholderLabel.setAnchorPoint(cc.p(0, 1)); this._placeholderLabel.setColor(cc.Color.GRAY); this._editBox.addChild(this._placeholderLabel, 100); - + this._updateLabelSize(editBoxSize); }, _updateLabelSize: function (editBoxSize) { @@ -376,8 +376,7 @@ EditBoxImpl.prototype = { }, setInputFlag: function (inputFlag) { - if ((this._edTxt.value !== this._placeholderText) - && (inputFlag === InputFlag.PASSWORD)) + if ((this._edTxt.value !== this._placeholderText) && (inputFlag === InputFlag.PASSWORD)) this._edTxt.type = 'password'; else this._edTxt.type = 'text'; @@ -662,7 +661,6 @@ _ccsg.EditBox = _ccsg.Node.extend({ this.initWithSizeAndBackgroundSprite(size, normal9SpriteBg); this._registerTouchEvent(); - var self = this; this.scheduleOnce(function () { editBoxImpl.registerClickEvent(); }, 0.01);