Merge pull request #388 from zilongshanren/improve-editbox

Improve editbox
This commit is contained in:
Jare Guo
2016-02-26 18:17:39 +08:00
4 changed files with 121 additions and 50 deletions
+73 -22
View File
@@ -44,12 +44,14 @@ var EditBox = cc.Class({
},
properties: {
_useOriginalSize: true,
_string: '',
/**
* Input string of EditBox.
* @property {String} string
*/
string: {
tooltip: 'i18n:COMPONENT.editbox.string',
get: function () {
return this._sgNode.string;
},
@@ -63,13 +65,20 @@ var EditBox = cc.Class({
* @property {cc.SpriteFrame} backGroundImage
*/
backgroundImage: {
tooltip: 'i18n:COMPONENT.editbox.backgroundImage',
default: null,
type: cc.SpriteFrame,
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();
}
}
},
@@ -81,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;
@@ -93,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.
@@ -107,6 +118,7 @@ var EditBox = cc.Class({
* @default InputMode.ANY
*/
inputMode: {
tooltip: 'i18n:COMPONENT.editbox.input_mode',
default: InputMode.ANY,
type: InputMode,
notify: function() {
@@ -119,17 +131,31 @@ var EditBox = cc.Class({
* @property {Number} fontSize
*/
fontSize: {
tooltip: 'i18n:COMPONENT.editbox.font_size',
default: 20,
notify: function() {
this._sgNode.fontSize = this.fontSize;
}
},
/**
* Change the lineHeight of displayed text.
* @property {Number} lineHeight
*/
lineHeight: {
tooltip: 'i18n:COMPONENT.editbox.line_height',
default: 40,
notify: function() {
this._sgNode.setLineHeight(this.lineHeight);
}
},
/**
* Font color of the input text.
* @property {cc.Color} fontColor
*/
fontColor: {
tooltip: 'i18n:COMPONENT.editbox.font_color',
default: cc.Color.WHITE,
notify: function() {
this._sgNode.fontColor = this.fontColor;
@@ -137,35 +163,38 @@ var EditBox = cc.Class({
},
/**
* The display text of placeHolder.
* @property {String} placeHolder
* The display text of placeholder.
* @property {String} placeholder
*/
placeHolder: {
placeholder: {
tooltip: 'i18n:COMPONENT.editbox.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: {
tooltip: 'i18n:COMPONENT.editbox.placeholder_font_size',
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: {
tooltip: 'i18n:COMPONENT.editbox.placeholder_font_color',
default: cc.Color.GRAY,
notify: function() {
this._sgNode.placeHolderFontColor = this.placeHolderFontColor;
this._sgNode.placeholderFontColor = this.placeholderFontColor;
}
},
@@ -174,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;
@@ -216,15 +246,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(cc.size(160, 40), 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 +268,30 @@ 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);
},
editBoxEditingDidBegan: function() {
cc.Component.EventHandler.emitEvents(this.editingDidBegan);
},
+40 -22
View File
@@ -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
@@ -228,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);
@@ -237,18 +244,18 @@ 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) {
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 () {
@@ -359,20 +376,17 @@ 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';
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();
@@ -647,7 +661,6 @@ _ccsg.EditBox = _ccsg.Node.extend({
this.initWithSizeAndBackgroundSprite(size, normal9SpriteBg);
this._registerTouchEvent();
var self = this;
this.scheduleOnce(function () {
editBoxImpl.registerClickEvent();
}, 0.01);
@@ -695,8 +708,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 +889,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 +1000,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);
+1 -1
View File
@@ -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';
+7 -5
View File
@@ -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;