Cocos2d-x 3.4 之 文本输入之 CCTextFieldTTF

Cocos2d-x 3.4 之 文本输入之 CCTextFieldTTF,第1张

概述***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************** 本文主要讲述文本输入的东东。 这个的作用就不用多说,还是很重要的, cocos2d-x 引擎对于文本输入 有两个类可以实现, 一个是 —— CCTextField

***************************************转载请注明出处: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,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

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

创建文本框

// 创建文本框1textEdit = 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 )      {          //d出软键盘          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 就到这里了,

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

有些内容没有写出来,

完整版测试内容 -> 这里




********************************************

总结

以上是内存溢出为你收集整理的Cocos2d-x 3.4 之 文本输入之 CCTextFieldTTF全部内容,希望文章能够帮你解决Cocos2d-x 3.4 之 文本输入之 CCTextFieldTTF所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1053475.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存