CCLabel添加 显示下划线的api

来源:互联网 发布:2017mac mini会更新吗 编辑:程序博客网 时间:2024/06/07 09:21

项目中用到富文本,富文本就要用到下划线,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