Not showing font mentioned in textfield

I have a text field like this -

email = cocos2d::TextFieldTTF::textFieldWithPlaceHolder("hello",cocos2d::Size(0,0),TextHAlignment::LEFT, "fonts/A little sunshine.ttf", 48);


email->setPosition(Vec2(button4->getPositionX(),
                        button4->getPositionY()));

email->setColorSpaceHolder(Color3B(236,231,221));
email->setTextColor(Color4B(255,255,255,255));

email->setColor(Color3B(151,169,58));

email->setDelegate(this);
email->setVisible(false);
this->addChild(email);


auto touchListener = EventListenerTouchOneByOne::create();

touchListener->onTouchBegan = [](cocos2d::Touch* touch, cocos2d::Event * event)->bool {
    try {
        // Show the on screen keyboard
        auto email = dynamic_cast<TextFieldTTF *>(event->getCurrentTarget());
        email->attachWithIME();
        return true;
    }
    catch(std::bad_cast & err){
        return true;
    }
};

_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, email);

But its not showing the font mentioned, can anyone tell me why? I tried loading the font in labels and it loads fine, so there is no problem with font.

Or is there any other way to add textfield…

@slackmoehrle, can you please verify… :smiley:

and you have this font in resources? Maybe try with no spaces in the name?

Yes, i have that font in resources, i use it same way for labels as well, and it works. also tried other font with textfield but didn’t work.

Perhaps there is something here: http://cocos2d-x.org/programmersguide/6/

We should work on this chapter a bit more, I think.

Hello. I reproduced this bug on win32, but on mac don’t. I think this problem in “old way” font texture generation (in this case font texture will be generated by platform, not by freetype lib). I see at least two quick solutions:

  1. Use ui::TextField instead (from ui/UITextField.h), but ui::TextField have a little bug, with additional texture creation :grinning:

  2. Or better specify use of freetype directly ( In this case not happens texture creation by platform, because durty flag will reset only in next frame):

    email = cocos2d::TextFieldTTF::textFieldWithPlaceHolder(“hello”, “fonts/A little sunshine.ttf”, 48);
    cocos2d::TTFConfig config = email->getTTFConfig();
    config.fontFilePath = “fonts/A little sunshine.ttf”;
    config.fontSize = 48;
    email->setTTFConfig(config);

2 Likes

Thanks @Alexander_Boriskov… :smiley: