您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x 3.4 之 文本输入之 CCTextFieldTTF

2015-04-08 14:09 483 查看
***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************

本文主要讲述文本输入的东东。

这个的作用就不用多说,还是很重要的,

cocos2d-x 引擎对于文本输入 有两个类可以实现,

一个是 —— CCTextFieldTTF

另一个是 —— CCEditBox

本文就主要说一下 第一个 CCTextFieldTTF,在 文本输入<2> 的时候再说 CCEditBox。

一、简介

这个类,在3.4 API 中的继承图是酱紫的:



可以看到,这个类继承自 Label 和 IMEDelegate(为子类提供虚拟键盘功能),

所以也有种说法,它就是个通过监听输入的字符而时时更新的Label而已。

二、相关操作

1. 创建

要注意,这里的创建并不是用 create了,但有两种方式

第一种,就是这样的,自己定义文本框大小、对齐方式

/** creates a TextFieldTTF from a fontname, alignment, dimension and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);

参数分别是:

placeholder  文本框默认内容(没有字符的时候的内容) 

dimensions  文本框大小

alignment    文本框内容对齐方式

fontName    文本框采用的字体

fontSize      文本框字体大小

PS:关于 TextHAlignment 有

enum class CC_DLL TextHAlignment
{
LEFT,
CENTER,
RIGHT
};


第二种就是,只定义字体种类、大小 和 默认内容,大小等于Label大小,如果内容超过编辑框大小会自动扩展:

/** creates a TextFieldTTF from a fontname and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);

参数和上面的意义一样,就不写了。

2.其他操作

<1> 设置文本框默认内容,默认内容的字体颜色

// 设置/获取 文本框默认内容
virtual void setPlaceHolder(const std::string& text);
virtual const std::string& getPlaceHolder() const;
// 设置/获取 文本框默认内容颜色
virtual const Color4B& getColorSpaceHolder();

virtual void setColorSpaceHolder(const Color3B& color);
virtual void setColorSpaceHolder(const Color4B& color);


<2>  编辑框内容

// 设置/获取 编辑框内容
virtual void setString(const std::string& text) override;
virtual const std::string& getString() const override;
// 设置 编辑框内容颜色
virtual void setTextColor(const Color4B& textColor) override;


<3> 虚拟键盘

//开启虚拟键盘
virtual bool attachWithIME();
//关闭虚拟键盘
virtual bool detachWithIME();


<4> 密码

// 如果输入的是密码,让输入的东西全是 * ,一个字母是一个*,一个汉字是3个*
textEdit->setSecureTextEntry(true);


<5> 字符个数

// 获取 文本框内容字符个数
inline int getCharCount() const { return _charCount; };


至于其他的一些操作,自行看API吧...

这里就不再赘述

三、Do it

现在来一个小例子,展现一下吧

创建文本框

// 创建文本框1
textEdit = CCTextFieldTTF::textFieldWithPlaceHolder("Please input:","Arial", 36);
textEdit->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
textEdit->setColorSpaceHolder(Color3B::BLUE);
this->addChild(textEdit);


设置4个按钮,对文本框进行操作:

// 文字按钮——密码 模式
auto btn_psw = MenuItemFont::create("password",CC_CALLBACK_1(TextInput::menupswCallback,this));
btn_psw->setPosition(Vec2(btn_psw->getContentSize().width,visibleSize.height/4));

// 文字按钮——展示 文本框内容
auto btn_show = MenuItemFont::create("show",CC_CALLBACK_1(TextInput::menushowCallback,this));
btn_show->setPosition(Vec2(btn_psw->getContentSize().width*2,visibleSize.height/4));

// 文字按钮——计算文本框内字符个数
auto btn_count = MenuItemFont::create("count",CC_CALLBACK_1(TextInput::menucountCallback,this));
btn_count->setPosition(Vec2(btn_psw->getContentSize().width,visibleSize.height/6));

// 文字按钮——改变文本框内字体颜色
auto btn_color = MenuItemFont::create("color",CC_CALLBACK_1(TextInput::menuchangeColorCallback,this));
btn_color->setPosition(Vec2(btn_psw->getContentSize().width*2,visibleSize.height/6));


相应函数的设定:

// 触摸事件
bool TextInput::onTouchBegan(CCTouch* touch, CCEvent* ev)
{
//用于判断是否点中了控件
bool isClicked = textEdit->boundingBox().containsPoint(touch->getLocation());

//如果点中了控件
if( isClicked )
{
//弹出软键盘
textEdit->attachWithIME();
}
else
{
textEdit->detachWithIME();
}

//表示接受触摸消息
return true;
}

// 是否设置成 密码 模式
void TextInput::menupswCallback(cocos2d::Ref* pSender)
{
if( textEdit->isSecureTextEntry() )
{
textEdit->setSecureTextEntry(false);
}
else
{
textEdit->setSecureTextEntry(true);
}
}

// 展示文本框内容
void TextInput::menushowCallback(cocos2d::Ref* pSender)
{
auto label = (Label*) this->getChildByTag(111);
label->setString(textEdit->getString());
}

// 计算文本框内容字符个数
void TextInput::menucountCallback(cocos2d::Ref* pSender)
{
auto label = (Label*) this->getChildByTag(112);
label->setString( StringUtils::format(" %d ",textEdit->getCharCount()) );
}

// 改变文本框字体颜色
void TextInput::menuchangeColorCallback(cocos2d::Ref* pSender)
{
textEdit->setColor(Color3B::GREEN);
}


Ok,运行一下(界面有点渣呀。。小测试,不要在意这些细节。。)



关于CCTextFieldTTF 就到这里了,

测试小例子的全部内容并非只有这些。。

有些内容没有写出来,

完整版测试内容  ->  这里
 

***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息