cocos代码研究(21)Widget子类Text,TextAtlas,TextBMFont学习笔记

理论基础

Text类又称ttf格式文本,可以用ttf文件或者系统自带字体,支持文字多,但是ttf文件格式体积大,渲染速度慢;

TextBMFont类又称fnt格式文本,纹理创建,根据纹理上有的文字来显示;

TextAtlas类又称艺术字文本;

代码实践

static TextBMFont * create()
创建TextBMFont对象。

static TextBMFont * create(const std::string &text, //文本的字符串形式。
const std::string &filename) //位图字体的文件路径。
通过指定的文本,以及位图字体的路径,创建TextBMFont对象。

void setFntFile (const std::string &fileName)
设置位图字体文件。

void setText (const std::string &value)
设置TextBMFont显示的字符串。

void setString (const std::string &value)
设置TextBMFont显示的字符串。

const std::string & getStringValue () const
获取TextBMFont当前显示的字符串。

const std::string & getString () const
获取TextBMFont当前显示的字符串。

ssize_t getStringLength () const
获取字符串长度。

Text类实例:

        // Create the line wrap
        Text* text = Text::create("TextArea Widget can line wrap",
                                  "AmericanTypewriter",32);
        text->ignoreContentAdaptWithSize(false);
        text->setContentSize(Size(280, 150));
        text->setTextHorizontalAlignment(TextHAlignment::CENTER);
        text->setTouchScaleChangeEnabled(true);
        text->setTouchEnabled(true);
        text->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type)
        {
            if (type == Widget::TouchEventType::ENDED)
            {
                if ((int)text->getContentSize().width == 280)
                {
                    text->setContentSize(Size(380,100));
                }else
                {
                    text->setContentSize(Size(280, 150));
                }
            }
        });
        text->setPosition(Vec2(widgetSize.width / 2.0f,
                               widgetSize.height / 2.0f
                               - text->getContentSize().height / 8.0f));
        _uiLayer->addChild(text);

TextAtlas类实例:

        // Create the text atlas
        TextAtlas* textAtlas = nullptr;
        if (Director::getInstance()->getWinSizeInPixels().height > 320.f)
        {
            textAtlas = TextAtlas::create("1234567890", "cocosui/labelatlas.png", 34, 44, "0");
        }
        else
        {
            textAtlas = TextAtlas::create("1234567890", "cocosui/labelatlas.png", 17, 22, "0");
        }
        textAtlas->setPosition(Vec2((widgetSize.width) / 2, widgetSize.height / 2.0f));
        _uiLayer->addChild(textAtlas);  

TextBMFont类实例:

        Size widgetSize = _widget->getContentSize();
        
        Text* alert = Text::create("TextBMFont","TextBMFont",30);
        alert->setColor(Color3B(159, 168, 176));
        alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
        _uiLayer->addChild(alert);
        
        // Create the TextBMFont
        TextBMFont* textBMFont = TextBMFont::create("BMFont", "cocosui/bitmapFontTest2.fnt");
        textBMFont->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2.0f + textBMFont->getContentSize().height / 8.0f));
        _uiLayer->addChild(textBMFont);
原文地址:https://www.cnblogs.com/damowang/p/4862124.html