cocos2d-x-3.3rc2 字体

Label

class CC_DLL Label : public SpriteBatchNode, public LabelProtocol


定义:

     /** Creates a label with an initial string,font[font name or font file],font size, dimension in points, horizontal alignment and vertical alignment.
     * @warning It will generate texture by the platform-dependent code
     */
    static Label* createWithSystemFont(const std::string& text, const std::string& font, float fontSize,
        const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
        TextVAlignment vAlignment = TextVAlignment::TOP);

    /** Creates a label with an initial string,font file,font size, dimension in points, horizontal alignment and vertical alignment.
     * @warning Not support font name.
     * @warning Cache textures for each different font size or font file.
     */
    static Label * createWithTTF(const std::string& text, const std::string& fontFile, float fontSize,
        const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
        TextVAlignment vAlignment = TextVAlignment::TOP);

    /** Create a label with TTF configuration
     * @warning Not support font name.
     * @warning Cache textures for each different font file when enable distance field.
     * @warning Cache textures for each different font size or font file when disable distance field.
     */
    static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment = TextHAlignment::LEFT, int maxLineWidth = 0);
    
    /* Creates a label with an FNT file,an initial string,horizontal alignment,max line width and the offset of image*/
    static Label* createWithBMFont(const std::string& bmfontFilePath, const std::string& text,
        const TextHAlignment& alignment = TextHAlignment::LEFT, int maxLineWidth = 0, 
        const Vec2& imageOffset = Vec2::ZERO);
    
    static Label * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
    static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
    static Label * createWithCharMap(const std::string& plistFile);
 

// FIXME:: If any of these enums are edited and/or reordered, update Texture2D.m
//! Vertical text alignment type
enum class CC_DLL TextVAlignment
{
    TOP,
    CENTER,
    BOTTOM
};

// FIXME:: If any of these enums are edited and/or reordered, update Texture2D.m
//! Horizontal text alignment type
enum class CC_DLL TextHAlignment
{
    LEFT,
    CENTER,
    RIGHT
};


说明


创建一个初始化了的字符串,font[字体的名字或文件],字体大小, 点的维数, 水平或垂直


createWithSystemFont()会通过平台依赖代码来调用原生API。前面三个形参是必须的,如果你忘记剩下的,他们都有默认值。

    auto blockSize = Size(s.width/3, 200);
    float fontSize = 26;

    auto top = Label::createWithSystemFont(pFont, pFont, 24);
    auto left = Label::createWithSystemFont("alignment left", pFont, fontSize,
                                          blockSize, TextHAlignment::LEFT, TextVAlignment::TOP);
    auto center = Label::createWithSystemFont("alignment center", pFont, fontSize,
                                            blockSize, TextHAlignment::CENTER, TextVAlignment::CENTER);
    auto right = Label::createWithSystemFont("alignment right", pFont, fontSize,
                                           blockSize, TextHAlignment::RIGHT,TextVAlignment::BOTTOM );

createWithTTF()使用 libfreetype2创建字体。它将字符串里面的每个字符存储到缓存里,这样提高了创建字体的速度。

//第一种方式通过TTF文件创建LabelTTF
auto label = Label::createWithTTF("label test","fonts/Marker Felt.ttf",32);
label->setPosition(Point(size.width/2,size.height*0.6));
this->addChild(label);    
 
//第二种方式通过TTFconfig创建LabelTTF
TTFConfig label_config;
 
//用TTFConfig设置TTF文件, 这个值不能忽略
label_config.fontFilePath = "fonts/Marker Felt.ttf";
label_config.fontSize = 32;
 
//选择glyhps, 它包括DYNAMIC, NEHE, ASCII和CUSTOM
label_config.glyphs = GlyphCollection::DYNAMIC;
 
//设置customGlyphs为空,因为此处我们不用自定义glyphs.
label_config.customGlyphs = nullptr;
 
//打开或关闭distanceField
label_config.distanceFieldEnabled = false;
label_config.outlineSize = 0;
 
//通过label config创建LabelTTF
auto label_two = Label::createWithTTF(label_config, "label test");
label_two->setPosition(Point(size.width/2,size.height*0.5));
this->addChild(label_two);

createWithBMFont()适合显示特定的文字,通过预先将文字生成图片,提高了效率,但是不能支持全部中文

//创建一个BMFont标签, 第一个参数是fnt文件, 第二个是你的内容. 你的字符串里面的字符应该包含fnt 文件, 否则它不会正常显示。
 
auto bmfont = Label::createWithBMFont("fonts/gameover_score_num.fnt", "123456789");


createWithCharMap()  使用的文字不多,组合多,但是文字的编码是连续的,比如数字,或者英文字符  旧版的是LabelAtlas

它比BMFont要简单。通常用于数字。

131028558179873.png

//创建一个char map标签.
//parameters: PNG picture, width, height, the first char.
auto charMap = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
charMap->setPosition(Point(size.width/2,size.height*0.4));
charMap->setString("123456789");
this->addChild(charMap);
 
//你也可以用一个plist文件创建char map
auto charMap2 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
charMap2->setPosition(Point(Point(size.width/2,size.height*0.3)));
charMap2->setString("123456789");
this->addChild(charMap2);

p3


发光、阴影和轮廓效果

// 轮廓效果,只支持TTF
label_two->enableOutline(Color4B(255,0,0,255),5);
 
// 发光效果,只支持TTF。并且distanceFieldEnabled必须为真
label_three->enableGlow(Color4B(255,0,0,255));
 
// 阴影效果
label_four->enableShadow(Color4B(0,0,255,255),Size(3,10),0);




猜你喜欢

转载自blog.csdn.net/wu745187355/article/details/42970327