<p> 感谢:<a target=_blank href="http://www.cocoachina.com/bbs/read.PHP?tid-312569.html">quick-3.5获取RichText高度</a></p><p>引擎版本:3.3</p><p>在布局RichText的时候,需要获得RichText的实际渲染高度,然后根据实际高度摆放位置才不会有大的偏差。</p><p>改动UIRichText.cpp,就一个函数,改动一句,加一句,具体如下,见高亮注释:</p>
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,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]; } 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); <strong style="background-color: rgb(255,204,102);">// custom:*** 2015.12.1,加上行间距 newContentSizeHeight += _verticalSpace;</strong> 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; } } <span style="background-color: rgb(255,102);"><strong> // custom:*** 2015.12.1,重新设置高度 //_elementRenderersContainer->setContentSize(_contentSize); _elementRenderersContainer->setContentSize(Size(_contentSize.width,newContentSizeHeight));</strong></span> delete [] maxHeights; }
注意:
1、需要先调用richText->formatText();,如下:
richText->formatText(); auto bbb = richText->getVirtualRendererSize();// 就可以获得高度了。