原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的CrossApp之旅系列]
博客地址:http://blog.csdn.net/qq446569365
RichText 是Cocos GUI功能中的一个控件,主要用于图文混编。称之为“富文本”。然后笔者在使用其进行开发的过程中发现他居然不回自动更改控件的高度。下边空出一大块范围,由于我是用其开发聊天系统,下边空出一行实在是无法忍受的,所以开始对源代码动刀子,尝试添加个功能。在添加过程中发现,触控团队应该是有让他自动适应高度的功能,但是不知道为啥却没有实现(目测是在代码里少打了一行……)笔者修复了一下,(菜鸟级修复,大神请别喷,如果有更好的方案,请务必回复告知,跪谢!)。
修复方法很简单,找到ui/UIRichText.cpp文件中的formarRenderers方法(OMG,触控的各位大牛,咱不至于把 t 按成 r 吧……),
_customSize.height=newContentSizeHeight;
_elementRenderersContainer->setContentSize(_size);
并修改一行:
_elementRenderersContainer->setPosition(0,0);
即可!如果本篇博客就这么完了,我相信读者一定要打死我了!所以,附上代码段……
3.0版本代码段:
void RichText::formarRenderers() { if (_ignoreSize) { float newContentSizeWidth = 0.0f; float newContentSizeHeight = 0.0f; Vector<Node*>* row = (_elementRenders[0]); float nextPosX = 0.0f; for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); l->setAnchorPoint(Point::ZERO); l->setPosition(Point(nextPosX,0.0f)); _elementRenderersContainer->addChild(l,1,(int)j); Size iSize = l->getContentSize(); newContentSizeWidth += iSize.width; newContentSizeHeight = MAX(newContentSizeHeight,iSize.height); nextPosX += iSize.width; } _elementRenderersContainer->setContentSize(Size(newContentSizeWidth,newContentSizeHeight)); } else { float newContentSizeHeight = 0.0f; float *maxHeights = new float[_elementRenders.size()]; for (size_t i=0; i<_elementRenders.size(); i++) { Vector<Node*>* row = (_elementRenders[i]); float maxHeight = 0.0f; for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); maxHeight = MAX(l->getContentSize().height,maxHeight); } maxHeights[i] = maxHeight; newContentSizeHeight += maxHeights[i]; } _customSize.height=newContentSizeHeight;//------------------添加的第一行------------------ float nextPosY = _customSize.height; for (size_t i=0; i<_elementRenders.size(); i++) { Vector<Node*>* row = (_elementRenders[i]); float nextPosX = 0.0f; nextPosY -= (maxHeights[i] + _verticalSpace); for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); l->setAnchorPoint(Point::ZERO); l->setPosition(Point(nextPosX,nextPosY)); _elementRenderersContainer->addChild(l,(int)(i*10 + j)); nextPosX += l->getContentSize().width; } } _elementRenderersContainer->setContentSize(_size); delete [] maxHeights; } size_t length = _elementRenders.size(); for (size_t i = 0; i<length; i++) { Vector<Node*>* l = _elementRenders[i]; l->clear(); delete l; } _elementRenders.clear(); if (_ignoreSize) { Size s = getVirtualRendererSize(); _size = s; } else { _size = _customSize; } updateContentSizeWithTextureSize(_size); _elementRenderersContainer->setContentSize(_size);//------------------添加的第二行------------------ _elementRenderersContainer->setPosition(0,0);//------------------修改这行------------------ //最后这行原来是 _elementRenderersContainer->setPosition(_contentSize.width / 2.0f,_contentSize.height / 2.0f); }
3.3版本从代码上看好像也存在这个问题,但是我没有实际运行测试过是否有这个问题,懒得运行了。各位如果3.3遇到的话,也可以这么修复,只不过3.3版本有一些变量名的变化而已。
3.3版本代码:
void RichText::formarRenderers() { if (_ignoreSize) { float newContentSizeWidth = 0.0f; float newContentSizeHeight = 0.0f; Vector<Node*>* row = (_elementRenders[0]); float nextPosX = 0.0f; for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); l->setAnchorPoint(Vec2::ZERO); l->setPosition(nextPosX,0.0f); _elementRenderersContainer->addChild(l,1); Size iSize = l->getContentSize(); newContentSizeWidth += iSize.width; newContentSizeHeight = MAX(newContentSizeHeight,maxHeight); } maxHeights[i] = maxHeight; newContentSizeHeight += maxHeights[i]; } this->_customSize.height=newContentSizeHeight;//------------------添加的第一行------------------ float nextPosY = _customSize.height; for (size_t i=0; i<_elementRenders.size(); i++) { Vector<Node*>* row = (_elementRenders[i]); float nextPosX = 0.0f; nextPosY -= (maxHeights[i] + _verticalSpace); for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); l->setAnchorPoint(Vec2::ZERO); l->setPosition(nextPosX,nextPosY); _elementRenderersContainer->addChild(l,1); nextPosX += l->getContentSize().width; } } _elementRenderersContainer->setContentSize(_contentSize); delete [] maxHeights; } size_t length = _elementRenders.size(); for (size_t i = 0; i<length; i++) { Vector<Node*>* l = _elementRenders[i]; l->clear(); delete l; } _elementRenders.clear(); if (_ignoreSize) { Size s = getVirtualRendererSize(); this->setContentSize(s); } else { this->setContentSize(_customSize); } updateContentSizeWithTextureSize(_contentSize); _elementRenderersContainer->setContentSize(_contentSize);//------------------添加的第二行------------------ _elementRenderersContainer->setPosition(0,_contentSize.height / 2.0f); }
改完之后保存编译即可! 原文链接:https://www.f2er.com/cocos2dx/345590.html