CCLabel: fix fontName attribute (#19095)

* add  method for FontAtlas

* break empty font

* reduce string constrution
This commit is contained in:
Arnold 2018-10-18 09:00:23 +08:00 committed by minggo
parent 7770234367
commit d63f554bf2
4 changed files with 20 additions and 2 deletions

View File

@ -477,11 +477,22 @@ Texture2D* FontAtlas::getTexture(int slot)
return _atlasTextures[slot];
}
void FontAtlas::setLineHeight(float newHeight)
void FontAtlas::setLineHeight(float newHeight)
{
_lineHeight = newHeight;
}
std::string FontAtlas::getFontName() const
{
std::string fontName = _fontFreeType ? _fontFreeType->getFontName() : "";
if(fontName.empty()) return fontName;
auto idx = fontName.rfind("/");
if (idx != std::string::npos) { return fontName.substr(idx + 1); }
idx = fontName.rfind("\\");
if (idx != std::string::npos) { return fontName.substr(idx + 1); }
return fontName;
}
void FontAtlas::setAliasTexParameters()
{
if (_antialiasEnabled)

View File

@ -84,6 +84,8 @@ public:
float getLineHeight() const { return _lineHeight; }
void setLineHeight(float newHeight);
std::string getFontName() const;
Texture2D* getTexture(int slot);
const Font* getFont() const { return _font; }

View File

@ -73,6 +73,7 @@ public:
int getFontAscender() const;
const char* getFontFamily() const;
std::string getFontName() const { return _fontName; }
virtual FontAtlas* createFontAtlas() override;
virtual int getFontMaxHeight() const override { return _lineHeight; }

View File

@ -2084,7 +2084,11 @@ void Label::removeChild(Node* child, bool cleanup /* = true */)
FontDefinition Label::_getFontDefinition() const
{
FontDefinition systemFontDef;
systemFontDef._fontName = _systemFont;
std::string fontName = _systemFont;
if (_fontAtlas && !_fontAtlas->getFontName().empty()) fontName = _fontAtlas->getFontName();
systemFontDef._fontName = fontName;
systemFontDef._fontSize = _systemFontSize;
systemFontDef._alignment = _hAlignment;
systemFontDef._vertAlignment = _vAlignment;