RichText获取实际高度

<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();// 就可以获得高度了。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...