開始學習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();