cocos2dx-3.2------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
来源:互联网 发布:iphone eve软件 编辑:程序博客网 时间:2024/06/05 05:06
下面简单介绍Label的用法
1、使用.ttf
当然了,也可以用Label创建普通的标签,效果和用CCLabelTTF::create()的一样
效果如图所示。这里有个地方要注意下,想要显示荧光效果,必须令distanceFieldEnabled = true,否则看不到效果。
在上面的代码中//A行是通过 createWithSystemFont 函数创建 Label 对象
代码//B行是通过 createWithTTF 是创建 TTF 字体标签对象
代码//C是 createWithBMFont 是创建位图字体标签对象
代码//D行TTFConfig ttfConfig("fonts/Marker Felt.ttf", 36, GlyphCollection::DYNAMIC)是创建一个TTFConfig结构体变量,前面已经介绍这个结构体了
代码//E行Label::createWithTTF(ttfConfig,"Hello World4") 是通过指定 TTFConfig 创建 TTF 字体标签
代码//F行代码 ttfConfig.outlineSize = 4 设置 TTFConfig 的描边字段
代码//G行代码 Label::createWithTTF(ttfConfig,"Hello World5") 是重新创建 TTF 字体标签。
代码//H行代码 label5->enableShadow(Color4B(255,255,255,128),Size(4, -4)) 是设置标签的阴影效果
代码//I行代码 label5->setColor(Color3B::RED) 是设置标签的颜色
1、使用.ttf
1)创建
- TTFConfig config2("fonts/Marker Felt.ttf",30);//初始化TTFConfig,第一个参数为字库的路径,第二个参数为字体大小
- auto label2 = Label::createWithTTF(config2,"createWithTTF",TextHAlignment::LEFT);//创建label,并向左对其
- label2->setPosition(Point(visibleSize.width/2,300));
- label2->setAnchorPoint(Point::ANCHOR_MIDDLE);//设置锚点居中
- this->addChild(label2,2);
- auto label4 = Label::create("create","Marker Felt",30);
2)另字体看起来紧凑点,也就是设置distanceFieldEnabled = true
直接修改config里的distanceFieldEnabled,方式如下:
- TTFConfig config2("fonts/Marker Felt.ttf",30,GlyphCollection::DYNAMIC,nullptr,true);//修改config的第五个参数为true
3)设置glow(荧光)效果
- label2->enableGlow(Color3B::GREEN);//荧光颜色为绿色
4)设置描边
- label2->enableOutline(Color4B(255,125,0,255),8);//第一个参数为描边的颜色,第二个参数为描边的大小
5)设置阴影
- label2->enableShadow(Color3B::RED,Size(2,-2),0.2,0.5);//第一个参数为阴影颜色,第二个参数为阴影相对于标签的坐标,第三个参数设置透明度,第四个参数与模糊有关
2、使用.fnt 的label
1)创建
- auto label3 = Label::createWithBMFont("fonts/bitmapFontTest.fnt", "createWithBMFont");
- label3->setPosition(Point(visibleSize.width/2,250));
- label3->setAnchorPoint(Point::ANCHOR_MIDDLE);
- this->addChild(label3,2);
- label3->enableShadow();
2)设置阴影(描边和荧光只能用在.ttf 上)
label3->enableShadow(Color3B::RED);
效果如图,可以与上图对比一下。
3、使用.png
加入我们有这么一张图,使用方法如下:
1)创建
- auto label4= Label::createWithCharMap("fonts/costFont.png",30,44,'/');//参数分别为:路径;每个字符的宽和高,起始字符
- label4->setPosition(Point(visibleSize.width/2,200));
- label4->setAnchorPoint(Point::ANCHOR_MIDDLE);
- label4->setString("10");//设置显示的内容为”10“this->addChild(label4,2);
2)设置阴影
label4->enableShadow(Color3B::RED);
4、取消所有特效
label->disableEffect();//取消所有特效
下面看一个整体的效果图
代码:
- bool HelloWorld::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- Size visibleSize = Director::getInstance()->getVisibleSize();
- Point origin = Director::getInstance()->getVisibleOrigin();
- auto closeItem = MenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
- closeItem->setPosition(Point(origin.x+ visibleSize.width - closeItem->getContentSize().width/2 ,
- origin.y + closeItem->getContentSize().height/2));
- auto menu = Menu::create(closeItem, NULL);
- menu->setPosition(Point::ZERO);
- this->addChild(menu, 1);
- autolabel1 = Label::createWithSystemFont("Hello World1","Arial", 36); //A
- label1->setPosition(Point(origin.x+ visibleSize.width/2,
- origin.y + visibleSize.height - 100));
- this->addChild(label1,1);
- autolabel2 = Label::createWithTTF("Hello World2", "fonts/MarkerFelt.ttf", 36); //B
- label2->setPosition(Point(origin.x+ visibleSize.width/2,
- origin.y + visibleSize.height - 200));
- this->addChild(label2,1);
- autolabel3 = Label::createWithBMFont("fonts/BMFont.fnt", "HelloWorld3"); //C
- label3->setPosition(Point(origin.x+ visibleSize.width/2,
- origin.y + visibleSize.height - 300));
- this->addChild(label3,1);
- TTFConfigttfConfig("fonts/Marker Felt.ttf",
- 36,
- GlyphCollection::DYNAMIC); //D
- autolabel4 = Label::createWithTTF(ttfConfig, "Hello World4"); //E
- label4->setPosition(Point(origin.x+ visibleSize.width/2,
- origin.y + visibleSize.height - 400));
- this->addChild(label4, 1);
- ttfConfig.outlineSize= 4; //F
- autolabel5 = Label::createWithTTF(ttfConfig, "Hello World5"); //G
- label5->setPosition(Point(origin.x+ visibleSize.width/2,
- origin.y + visibleSize.height - 500));
- label5->enableShadow(Color4B(255,255,255,128),Size(4, -4)); //H
- label5->setColor(Color3B::RED); //I
- this->addChild(label5,1);
- return true;
- }
在上面的代码中//A行是通过 createWithSystemFont 函数创建 Label 对象
代码//B行是通过 createWithTTF 是创建 TTF 字体标签对象
代码//C是 createWithBMFont 是创建位图字体标签对象
代码//D行TTFConfig ttfConfig("fonts/Marker Felt.ttf", 36, GlyphCollection::DYNAMIC)是创建一个TTFConfig结构体变量,前面已经介绍这个结构体了
代码//E行Label::createWithTTF(ttfConfig,"Hello World4") 是通过指定 TTFConfig 创建 TTF 字体标签
代码//F行代码 ttfConfig.outlineSize = 4 设置 TTFConfig 的描边字段
代码//G行代码 Label::createWithTTF(ttfConfig,"Hello World5") 是重新创建 TTF 字体标签。
代码//H行代码 label5->enableShadow(Color4B(255,255,255,128),Size(4, -4)) 是设置标签的阴影效果
代码//I行代码 label5->setColor(Color3B::RED) 是设置标签的颜色
0 0
- cocos2dx-3.2------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
- cocos2dx-3.0(5)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
- cocos2dx-3.0(6)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
- cocos2dx-3.0(7)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
- cocos2dx-3.0(8)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
- cocos2dx 文本label显示方法LabelBMFont,LabelAtlas,MenuItemFont
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas - 冠军
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas - 冠军
- Cocos2d-x中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- cocos2dx-3.1.1 labelttf与label
- LabelAtlas quick-cocos2dx中的艺术字的使用
- cocos2dx LabelTTF 标签
- cocos2dx之Label实现换行
- 黑马程序员---03-第三个UI程序
- 重启nginx后丢失nginx.pid,如何重新启动nginx
- iOS_绘制带删除线的Label
- Codeforces Round #262 (Div. 2) A,B,C
- ffmpeg结构体以及函数介绍(三)--usefully
- cocos2dx-3.2------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
- 朋友的居然出卖我
- Android根据包名获取应用程序的信息
- SilverLight中如何跟ActiveX控件交互使用
- 生产者消费者问题
- WPF/Windows Phone/WinRT: Trigger的没落和ViewStateManager的兴起
- 用的循环链表
- NBUT 1450
- 去掉^M