Merge pull request #6716 from andyque/issue4900

closed #4900, add TTF font to UIText
This commit is contained in:
minggo 2014-05-14 09:57:57 +08:00
commit 0303e5d270
2 changed files with 42 additions and 4 deletions

View File

@ -40,7 +40,8 @@ _fontName("Thonburi"),
_fontSize(10),
_onSelectedScaleOffset(0.5),
_labelRenderer(nullptr),
_labelRendererAdaptDirty(true)
_labelRendererAdaptDirty(true),
_type(Type::SYSTEM)
{
}
@ -121,8 +122,15 @@ ssize_t Text::getStringLength()
void Text::setFontSize(int size)
{
if (_type == Type::SYSTEM) {
_labelRenderer->setSystemFontSize(size);
}
else{
TTFConfig config = _labelRenderer->getTTFConfig();
config.fontSize = size;
_labelRenderer->setTTFConfig(config);
}
_fontSize = size;
_labelRenderer->setSystemFontSize(size);
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
}
@ -134,8 +142,18 @@ int Text::getFontSize()
void Text::setFontName(const std::string& name)
{
if(FileUtils::getInstance()->isFileExist(name))
{
TTFConfig config = _labelRenderer->getTTFConfig();
config.fontFilePath = name;
_labelRenderer->setTTFConfig(config);
_type = Type::TTF;
}
else{
_labelRenderer->setSystemFontName(name);
_type = Type::SYSTEM;
}
_fontName = name;
_labelRenderer->setSystemFontName(name);
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
}
@ -144,6 +162,11 @@ const std::string& Text::getFontName()
{
return _fontName;
}
Text::Type Text::getType() const
{
return _type;
}
void Text::setTextAreaSize(const Size &size)
{

View File

@ -41,6 +41,11 @@ class Text : public Widget
DECLARE_CLASS_GUI_INFO
public:
enum class Type
{
SYSTEM,
TTF
};
/**
* Default constructor
*/
@ -58,6 +63,9 @@ public:
/**
* create a Text object with textContent, fontName and fontSize
* the fontName could be a system font name or a TTF file path.
* Usage: Text *text = Text::create("Hello", "Arial", 20); //create a system font UIText
* Text *text = Text::create("Hello", "xxx\xxx.ttf", 20); //create a TTF font UIText
*/
static Text* create(const std::string& textContent,
const std::string& fontName,
@ -95,12 +103,18 @@ public:
/**
* Sets the font name of label.
*
* If you are trying to use a system font, you could just pass a font name
* If you are trying to use a TTF, you should pass a file path to the TTF file
* Usage: Text *text = Text::create("Hello", "Arial", 20); //create a system font UIText
* text->setFontName("Marfelt"); // it will change the font to system font no matter the previous font type is TTF or system font
* text->setFontName("xxxx/xxx.ttf"); //it will change the font to TTF font no matter the previous font type is TTF or system font
* @param font name.
*/
void setFontName(const std::string& name);
const std::string& getFontName();
Type getType() const;
/**
* Sets the touch scale enabled of label.
@ -169,6 +183,7 @@ protected:
float _onSelectedScaleOffset;
Label* _labelRenderer;
bool _labelRendererAdaptDirty;
Type _type;
};
}