void Label::alignText() { if (_fontAtlas == nullptr || _utf16Text.empty()) { setContentSize(Size::ZERO); return; } _fontAtlas->prepareLetterDefinitions(_utf16Text); auto& textures = _fontAtlas->getTextures(); if (textures.size() > _batchNodes.size()) { for (auto index = _batchNodes.size(); index < textures.size(); ++index) { auto batchNode = SpriteBatchNode::createWithTexture(textures.at(index)); if (batchNode) { _isOpacityModifyRGB = batchNode->getTexture()->hasPremultipliedAlpha(); _blendFunc = batchNode->getBlendFunc(); batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); batchNode->setPosition(Vec2::ZERO); _batchNodes.pushBack(batchNode); } } } if (_batchNodes.empty()) { return; } _reusedLetter->setBatchNode(_batchNodes.at(0)); _lengthOfString = 0; _textDesiredHeight = 0.f; _linesWidth.clear(); if (_maxLineWidth > 0.f && !_lineBreakWithoutSpaces) { multilineTextWrapByWord(); } else { multilineTextWrapByChar(); } computeAlignmentOffset(); updateQuads(); updateLabelLetters(); updateColor(); }
void Label::shrinkLabelToContentSize(const std::function<bool(void)>& lambda) { float fontSize = this->getRenderingFontSize(); int i = 0; auto letterDefinition = _fontAtlas->_letterDefinitions; auto tempLetterDefinition = letterDefinition; float originalLineHeight = _lineHeight; bool flag = true; while (lambda()) { ++i; float newFontSize = fontSize - i; flag = false; if (newFontSize <= 0) { break; } float scale = newFontSize / fontSize; std::swap(_fontAtlas->_letterDefinitions, tempLetterDefinition); _fontAtlas->scaleFontLetterDefinition(scale); this->setLineHeight(originalLineHeight * scale); if (_maxLineWidth > 0.f && !_lineBreakWithoutSpaces) { multilineTextWrapByWord(); } else { multilineTextWrapByChar(); } computeAlignmentOffset(); tempLetterDefinition = letterDefinition; } this->setLineHeight(originalLineHeight); std::swap(_fontAtlas->_letterDefinitions, letterDefinition); if (!flag) { if (fontSize - i >= 0) { this->scaleFontSizeDown(fontSize - i); } } }