CCLabel添加 显示下划线的api

项目中用到富文本,富文本就要用到下划线,cocos2dx的CCLabel 又没有现成的,所以只能自己操刀了

修改CCLabel的渲染部分

a:在draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)中加

_customCommand1.init(_globalZOrder);
 _customCommand1.func = CC_CALLBACK_0(Label::onDrawLine, this, transform, transformUpdated);
renderer->addCommand(&_customCommand1);


记得要新建一个_customCommand1  不能直接用_customCommand 不然会把原来要渲染的东西会抹掉,最直接的表现就是 文本渲染不出来 只渲染了 下划线


在draw()里边添加渲染,会有一个问题,如果创建文本的时候 没有找到字体(最常见的是传入字体的时候 没有加上字体后缀.ttf),CCLabel调用的是createWithSystemFont()来创建label,此时visit()里边的_textSprite  就不是null了,所以 CCLabel的draw()就调用不到了   画线也就没法用了


b:在draw()里边加有时有问题的话 就直接在visit()里边加

if (_isUnderLine)
{
bool transformUpdated = flags & FLAGS_TRANSFORM_DIRTY;
_customCommand.init(_globalZOrder, _modelViewTransform, flags);
_customCommand.func = CC_CALLBACK_0(Label::onDrawLine, this, _modelViewTransform, transformUpdated);
renderer->addCommand(&_customCommand);
}

func的参数 最后一项是个bool值,直接传入flags的话会有很多警告


void Label::onDrawLine(const Mat4& transform, bool transformUpdated)
{
Director* director = Director::getInstance();
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
glLineWidth( 5.0f );
DrawPrimitives::setDrawColor4B(255,0,0,255);
DrawPrimitives::drawLine(cocos2d::Point(0,0), cocos2d::Point(_contentSize.width,0));
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}


这样就能画线了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值