开始学习cocos2dx
TextFieldTTF 是可编辑文本框;
用法:
TextFieldTTF *text = TextFieldTTF::textFieldWithPlaceHolder("<在这里输入>", "宋体", 30);
text->setPosition(visibleSize.width/2, visibleSize.height/2);
addChild(text);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan =[text](Touch *t,Event *e){
if(text->getBoundingBox().containsPoint(t->getLocation())){
text->attachWithIME();
}
else {
text->detachWithIME();
}
学习源码:继承关系
TextFieldTTF 同时继承Label和IMEDelegate; 其中IMEDelegate 是一个输入法的管理类。
打开TextFieldTTF.h文件 :看到不是TextFieldTTF 这个类 而是TextFieldDelegate这个类 ,这个类应该是TextFieldTTF这个类的一个管理类。
在.h文件中初始化的方法:
/** 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);
/** creates a LabelTTF from a fontname and font size */
static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
/** initializes the TextFieldTTF with a font name, alignment, dimension and font size */
bool initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
/** initializes the TextFieldTTF with a font name and font size */
bool initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
.cpp的初始方法的实现:
.h关联输入法的方法:
/**
@brief Open keyboard and receive input text.
*/
virtual bool attachWithIME();
/**
@brief End text input and close keyboard.
*/
virtual bool detachWithIME();