From 31a2194dcbe8d9578ab4db6b0bcfe98346bb81ff Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 13 May 2014 17:45:15 +0800 Subject: [PATCH 1/4] closed #4900, add TTF font to UIText --- cocos/ui/UIText.cpp | 29 ++++++++++++++++++++++++++--- cocos/ui/UIText.h | 8 ++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cocos/ui/UIText.cpp b/cocos/ui/UIText.cpp index a0fc15131a..c45ef913e9 100644 --- a/cocos/ui/UIText.cpp +++ b/cocos/ui/UIText.cpp @@ -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) { diff --git a/cocos/ui/UIText.h b/cocos/ui/UIText.h index 65d1d04ed9..53f66e9572 100644 --- a/cocos/ui/UIText.h +++ b/cocos/ui/UIText.h @@ -41,6 +41,11 @@ class Text : public Widget DECLARE_CLASS_GUI_INFO public: + enum class Type + { + SYSTEM, + TTF + }; /** * Default constructor */ @@ -101,6 +106,8 @@ public: void setFontName(const std::string& name); const std::string& getFontName(); + + Type getType() const; /** * Sets the touch scale enabled of label. @@ -169,6 +176,7 @@ protected: float _onSelectedScaleOffset; Label* _labelRenderer; bool _labelRendererAdaptDirty; + Type _type; }; } From 2a25afbb76ca092495bb7207d2cafc1f052a1639 Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 13 May 2014 18:15:53 +0800 Subject: [PATCH 2/4] closed #4900. add comment --- cocos/ui/UIText.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cocos/ui/UIText.h b/cocos/ui/UIText.h index 53f66e9572..2121f28ae8 100644 --- a/cocos/ui/UIText.h +++ b/cocos/ui/UIText.h @@ -100,6 +100,8 @@ public: /** * Sets the font name of label. + * If you are trying to use a system font, you could just pass a font name, for example: Arial + * If you are trying to use a TTF, you should pass a file path to the TTF file, for example: resource/XXX.ttf * * @param font name. */ From 92d9120967fe31c7bc3f2a044332f7eaa07d2a8b Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 14 May 2014 09:37:21 +0800 Subject: [PATCH 3/4] issue #4900, refactor --- cocos/ui/UIText.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cocos/ui/UIText.h b/cocos/ui/UIText.h index 2121f28ae8..72936269e4 100644 --- a/cocos/ui/UIText.h +++ b/cocos/ui/UIText.h @@ -63,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("xxx\xxx.ttf", "Arial", 20); //create a TTF font UIText */ static Text* create(const std::string& textContent, const std::string& fontName, @@ -100,9 +103,11 @@ public: /** * Sets the font name of label. - * If you are trying to use a system font, you could just pass a font name, for example: Arial - * If you are trying to use a TTF, you should pass a file path to the TTF file, for example: resource/XXX.ttf - * + * 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"); // call this method will change the system font + * text->setFontName("xxxx/xxx.ttf"); * @param font name. */ void setFontName(const std::string& name); From ca4a7a7e53d6074a38b7b065de1c61f1b7c251c9 Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 14 May 2014 09:55:55 +0800 Subject: [PATCH 4/4] issue #4900, refactor --- cocos/ui/UIText.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos/ui/UIText.h b/cocos/ui/UIText.h index 72936269e4..a9dd6d8482 100644 --- a/cocos/ui/UIText.h +++ b/cocos/ui/UIText.h @@ -65,7 +65,7 @@ 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("xxx\xxx.ttf", "Arial", 20); //create a TTF 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, @@ -106,8 +106,8 @@ public: * 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"); // call this method will change the system font - * text->setFontName("xxxx/xxx.ttf"); + * 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);