CCLabelTTF之换行

<版本:cocos2d-x——V2.2.1>

主角:CCLabelTTF

与换行相关的静态成员函数create:

CCLabelTTF * CCLabelTTF::create(
    const char *string,   // 显示字符串
    const char *fontName,  // 字体名
    float fontSize, // 字体大小
    const CCSize& dimensions, // 显示尺寸
    CCTextAlignment hAlignment); // 水平对齐方式
    // kCCTextAlignmentLeft左对齐
    // kCCTextAlignmentCenter居中
    // kCCTextAlignmentRight右对齐

CCLabelTTF* CCLabelTTF::create(
    const char *string,
    const char *fontName,
    float fontSize,
    const CCSize &dimensions,
    CCTextAlignment hAlignment, 
    CCVerticalTextAlignment vAlignment); // 垂直对齐方式
    // kCCVerticalTextAlignmentTop顶对齐
    // kCCVerticalTextAlignmentCenter居中
    // kCCVerticalTextAlignmentBottom底对齐

多行显示方式:

1.使用以上提到之外的其他create方法:在字符串参数中,插入'\n'字符

2.使用上述create方法:dimensions参数表示标签显示范围

对于第一种,我们需要控制到底应在何处插入'\n'字符,比较麻烦;选择第二种比较傻瓜式一点,方便使用

贴上示例代码(为了避免编码的问题,字符串使用纯英文):

bool HelloWorld::init()
{
    bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());

		CCSize size = CCDirector::sharedDirector()->getWinSize();

		string str = "Silent night, holy night,All is calm, all is bright."
			"Round your Virgin, Mother and Child.Holy infant so tender and mild,Sleep in heavenly peace,"
			"Sleep in heavenly peace.Silent night, holy night,"
			"Shepherds quake at the sight.Glories stream from heaven afar,"
			"Heavenly hosts sing Alleluia,Christ the Savior is born,"
			"Christ the Savior is born.";
		CCLabelTTF* label = CCLabelTTF::create(str.c_str(),"Arial",35,size,kCCTextAlignmentLeft);
		label->setAnchorPoint(CCPointZero);
		label->setPosition(CCPointZero);
		this->addChild(label);
		
		bRet = true;
	} while (0);
    
    return true;
}

效果图如下: 




















能力有限,希望对初学者有一丝丝的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值