cocos2d-x版本3.8
在RichText自动换行后,发现不能得到真实高度,查看源码发现算出了实际高度,但确没用上,都多少个版本了,还没完善
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); this->addProtectedChild(l,1); Size iSize = l->getContentSize(); newContentSizeWidth += iSize.width; newContentSizeHeight = MAX(newContentSizeHeight,iSize.height); nextPosX += iSize.width; } this->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); for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); l->setAnchorPoint(Vec2::ZERO); l->setPosition(nextPosX,nextPosY); this->addProtectedChild(l,1); nextPosX += l->getContentSize().width; } } 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); }
在RichText.h中加
Size _renderSize; Size& getRenderSize(); void setRenderSize(const Size &size);
在RichText.cpp中改formarRenderers()
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,newContentSizeHeight)); this->setRenderSize(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); for (ssize_t j=0; j<row->size(); j++) { Node* l = row->at(j); l->setAnchorPoint(Vec2::ZERO); l->setPosition(nextPosX,1); nextPosX += l->getContentSize().width; } } this->setRenderSize(Size(_contentSize.width,newContentSizeHeight));//这里把没用上的newContentSizeHeight用上 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); }
再实现
Size& RichText::getRenderSize() { return this->_renderSize; } void RichText::setRenderSize(const Size &size) { _renderSize = size; }
OK了,实际高度有了,发现getReaderSize()还是不对,继续查源码
发现adaptRenderers()是在Widget的visit()里调用的,也就是说当前桢不会被调用,这就好办了,在RichText pushBackElement 之后显示的调用一下formatText()就OK了,