cocos2d-lua:Label的使用
目录
Description
创建系统字体的Label
创建TTF字体的Label
方式1
方式2
Description
在Cocos2d-x中,创建Label有四种方式:
系统字体:createWithSystemFont
TTF字体:createWithTTF
BMFont:createWithBMFont
CharMap:createWithCharMap
这里记录一下在Cocos2d-lua中创建系统字体和TTF字体Label的使用!
创建系统字体的Label
--[[
static Label* createWithSystemFont(
const std::string& text, // 显示的文字内容
const std::string& font, // 字体文件或字体名称
float fontSize, // 字号(> 0)
const Size& dimensions = Size::ZERO, //尺寸,宽高(高度为0时自动换行)
TextHAlignment hAlignment = TextHAlignment::LEFT, // 水平对齐方式
TextVAlignment vAlignment = TextVAlignment::TOP // 垂直对齐方式
);
]]
local systemLabel = cc.Label:createWithSystemFont(
"PHP is the best language in the world !",
"Arial",
20,
cc.size(150, 0),
cc.TEXT_ALIGNMENT_LEFT,
cc.VERTICAL_TEXT_ALIGNMENT_TOP
)
systemLabel:setAnchorPoint(0, 1)
systemLabel:setPosition(cc.p(0, 0))
parent:addChild(systemLabel)
创建TTF字体的Label
方式1
--[[
static Label * createWithTTF(
const std::string& text,
const std::string& fontFilePath, // ttf字体文件路径
float fontSize,
const Size& dimensions = Size::ZERO,
TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP
);
]]
local ttfLabel = cc.Label:createWithTTF(
"PHP is the best language in the world !",
"res/fontFilePath.ttf",
20,
cc.size(150, 0),
cc.TEXT_ALIGNMENT_LEFT,
cc.VERTICAL_TEXT_ALIGNMENT_TOP
)
-- ttfLabel:setDimensions(150, 0) -- 设置Label的尺寸,height为0时自动换行
ttfLabel:setLineHeight(30) -- 设置行间距,不支持系统字体
ttfLabel:setAdditionalKerning(0) -- 设置文字间距,不支持系统字体
print("rows->", ttfLabel:getStringNumLines()) -- 获取行数
print("height->", ttfLabel:getContentSize().height) -- 获取Label实际的高度
ttfLabel:setAnchorPoint(0, 1)
ttfLabel:setPosition(cc.p(0, 0))
parent:addChild(ttfLabel)
方式2
--[[
// TTFConfig结构体
typedef struct _ttfConfig
{
std::string fontFilePath;
float fontSize;
GlyphCollection glyphs; // 字符集
const char *customGlyphs;
bool distanceFieldEnabled; // 是否让字体紧凑
int outlineSize; // 描边尺寸,描边颜色默认为黑色
// 构造函数
_ttfConfig(const std::string& filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0)
: fontFilePath(filePath)
, fontSize(size)
, glyphs(glyphCollection)
, customGlyphs(customGlyphCollection)
, distanceFieldEnabled(useDistanceField)
, outlineSize(outline)
{
if(outline > 0)
{
distanceFieldEnabled = false;
}
}
} TTFConfig;
static Label* createWithTTF(
const TTFConfig& ttfConfig, // TTFConfig配置
const std::string& text,
TextHAlignment hAlignment = TextHAlignment::LEFT,
int maxLineWidth = 0 // 最大行宽,用于自动换行,0表示不设置!
);
]]
local ttfConfig = {}
ttfConfig.fontFilePath = "res/fontFilePath.ttf"
ttfConfig.fontSize = 20
-- ttfConfig.outlineSize = 2
local ttfLabel = cc.Label:createWithTTF(
ttfConfig,
"PHP is the best language in the world !",
cc.TEXT_ALIGNMENT_LEFT,
150
)
ttfLabel:setTextColor(cc.c4b(255, 0, 0, 255)) -- 设置文本颜色
ttfLabel:enableOutline(cc.c4b(0, 255, 0, 255), 2) -- 设置描边
ttfLabel:setAnchorPoint(0, 1)
ttfLabel:setPosition(cc.p(0, 0))
parent:addChild(ttfLabel)
参考:cocos2dx[3.2](14)——新字体标签Label