新手记录 自学cocos2dx 之 TextFieldTTF

本文详细介绍了Cocos2d-x中TextFieldTTF组件的使用方法,包括如何创建可编辑文本框及与输入法交互的过程,并解析了其继承关系和技术实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始学习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();
 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值