UIText.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UILABEL_H__
  22. #define __UILABEL_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. #include "base/ccTypes.h"
  26. /**
  27. * @addtogroup ui
  28. * @{
  29. */
  30. NS_CC_BEGIN
  31. class Label;
  32. class Sprite;
  33. namespace ui {
  34. /**
  35. * For creating a system font or a TTF font Text
  36. */
  37. class CC_GUI_DLL Text : public Widget, public cocos2d::BlendProtocol
  38. {
  39. DECLARE_CLASS_GUI_INFO
  40. public:
  41. /** Type Text type.
  42. */
  43. enum class Type
  44. {
  45. SYSTEM,
  46. TTF
  47. };
  48. /**
  49. * Default constructor.
  50. * @js ctor
  51. * @lua new
  52. */
  53. Text();
  54. /**
  55. * Default destructor.
  56. * @js NA
  57. * @lua NA
  58. */
  59. virtual ~Text();
  60. /**
  61. * Create a Text object.
  62. *
  63. * @return An autoreleased Text object.
  64. */
  65. static Text* create();
  66. /**
  67. * Create a Text object with textContent, fontName and fontSize.
  68. * The fontName could be a system font name or a TTF file path.
  69. * Usage:
  70. * @code
  71. * //create a system font UIText.
  72. * Text *text = Text::create("Hello", "Arial", 20);
  73. * //create a TTF font UIText.
  74. * Text *text = Text::create("Hello", "xxx\xxx.ttf", 20);
  75. * @endcode
  76. *
  77. * @param textContent Text content string.
  78. * @param fontName A given font name.
  79. * @param fontSize A given font size.
  80. * @return An autoreleased Text object.
  81. */
  82. static Text* create(const std::string& textContent,
  83. const std::string& fontName,
  84. float fontSize);
  85. /**
  86. * Changes the string value of label.
  87. *
  88. * @param text String value.
  89. */
  90. CC_DEPRECATED_ATTRIBUTE void setText(const std::string& text)
  91. {
  92. this->setString(text);
  93. }
  94. void setString(const std::string& text);
  95. /**
  96. * Gets the string value of label.
  97. *
  98. * @return String value.
  99. */
  100. CC_DEPRECATED_ATTRIBUTE const std::string& getStringValue()
  101. {
  102. return this->getString();
  103. }
  104. const std::string& getString()const;
  105. /**
  106. * Gets the string length of the label.
  107. * Note: This length will be larger than the raw string length,
  108. * if you want to get the raw string length,
  109. * you should call this->getString().size() instead.
  110. *
  111. * @return String length.
  112. */
  113. ssize_t getStringLength()const;
  114. /**
  115. * Sets the font size of label.
  116. *
  117. * @param size The font size.
  118. */
  119. void setFontSize(float size);
  120. /**
  121. * Gets the font size of label.
  122. *
  123. * @return The font size.
  124. */
  125. float getFontSize()const;
  126. /**
  127. * Sets the font name of label.
  128. * If you are trying to use a system font, you could just pass a font name
  129. * If you are trying to use a TTF, you should pass a file path to the TTF file
  130. * Usage:
  131. * @code
  132. * //create a system font UIText
  133. * Text *text = Text::create("Hello", "Arial", 20);
  134. * // it will change the font to system font no matter the previous font type is TTF or system font
  135. * text->setFontName("Marfelt");
  136. * //it will change the font to TTF font no matter the previous font type is TTF or system font
  137. * text->setFontName("xxxx/xxx.ttf");
  138. * @endcode
  139. * @param name Font name.
  140. */
  141. void setFontName(const std::string& name);
  142. /** Gets the font name.
  143. *
  144. * @return Font name.
  145. */
  146. const std::string& getFontName()const;
  147. /** Gets the font type.
  148. * @return The font type.
  149. */
  150. Type getType() const;
  151. /**
  152. * Sets the touch scale enabled of label.
  153. *
  154. * @param enabled Touch scale enabled of label.
  155. */
  156. void setTouchScaleChangeEnabled(bool enabled);
  157. /**
  158. * Gets the touch scale enabled of label.
  159. *
  160. * @return Touch scale enabled of label.
  161. */
  162. bool isTouchScaleChangeEnabled()const;
  163. //override "getVirtualRendererSize" method of widget.
  164. virtual Size getVirtualRendererSize() const override;
  165. //override "getVirtualRenderer" method of widget.
  166. virtual Node* getVirtualRenderer() override;
  167. /** Gets the render size in auto mode.
  168. *
  169. * @return The size of render size in auto mode.
  170. */
  171. virtual Size getAutoRenderSize();
  172. /**
  173. * Returns the "class name" of widget.
  174. */
  175. virtual std::string getDescription() const override;
  176. /**
  177. * Sets the rendering size of the text, you should call this method
  178. * along with calling `ignoreContentAdaptWithSize(false)`, otherwise the text area
  179. * size is calculated by the real size of the text content.
  180. *
  181. * @param size The text rendering area size.
  182. *
  183. */
  184. void setTextAreaSize(const Size &size);
  185. /** Return the text rendering area size.
  186. *
  187. * @return The text rendering area size.
  188. */
  189. const Size& getTextAreaSize()const;
  190. /** Sets text horizontal alignment.
  191. *
  192. * @param alignment Horizontal text alignment type
  193. */
  194. void setTextHorizontalAlignment(TextHAlignment alignment);
  195. /** Gets text horizontal alignment.
  196. *
  197. * @return Horizontal text alignment type
  198. */
  199. TextHAlignment getTextHorizontalAlignment()const;
  200. /** Sets text vertical alignment.
  201. *
  202. * @param alignment vertical text alignment type
  203. */
  204. void setTextVerticalAlignment(TextVAlignment alignment);
  205. /** Gets text vertical alignment.
  206. *
  207. * @return Vertical text alignment type
  208. */
  209. TextVAlignment getTextVerticalAlignment()const;
  210. /** Sets text color.
  211. *
  212. * @param color Text color.
  213. */
  214. void setTextColor(const Color4B color);
  215. /** Gets text color.
  216. *
  217. * @return Text color.
  218. */
  219. const Color4B& getTextColor() const;
  220. /**
  221. * Enable shadow for the label.
  222. *
  223. * @todo support blur for shadow effect
  224. *
  225. * @param shadowColor The color of shadow effect.
  226. * @param offset The offset of shadow effect.
  227. * @param blurRadius The blur radius of shadow effect.
  228. */
  229. void enableShadow(const Color4B& shadowColor = Color4B::BLACK,
  230. const Size &offset = Size(2,-2),
  231. int blurRadius = 0);
  232. /**
  233. * Enable outline for the label.
  234. * It only works on IOS and Android when you use System fonts.
  235. *
  236. * @param outlineColor The color of outline.
  237. * @param outlineSize The size of outline.
  238. */
  239. void enableOutline(const Color4B& outlineColor,int outlineSize = 1);
  240. /** Only support for TTF.
  241. *
  242. * @param glowColor The color of glow.
  243. */
  244. void enableGlow(const Color4B& glowColor);
  245. /** Disable all text effects, including shadow, outline and glow.
  246. */
  247. void disableEffect();
  248. /**
  249. * Disable specific text effect.
  250. * Use LabelEffect parameter to specify which effect should be disabled.
  251. *
  252. * @see `LabelEffect`
  253. */
  254. void disableEffect(LabelEffect effect);
  255. /**
  256. * Return whether the shadow effect is enabled.
  257. */
  258. bool isShadowEnabled() const;
  259. /**
  260. * Return shadow effect offset value.
  261. */
  262. Size getShadowOffset() const;
  263. /**
  264. * Return the shadow effect blur radius.
  265. */
  266. float getShadowBlurRadius() const;
  267. /**
  268. * Return the shadow effect color value.
  269. */
  270. Color4B getShadowColor() const;
  271. /**
  272. * Return the outline effect size value.
  273. */
  274. int getOutlineSize() const;
  275. /**
  276. * Return current effect type.
  277. */
  278. LabelEffect getLabelEffectType() const;
  279. /**
  280. * Return current effect color value.
  281. */
  282. Color4B getEffectColor() const;
  283. /**
  284. * Provides a way to treat each character like a Sprite.
  285. * @warning No support system font.
  286. */
  287. virtual Sprite * getLetter(int lettetIndex);
  288. /**
  289. * Sets the source blending function.
  290. *
  291. * @param blendFunc A structure with source and destination factor to specify pixel arithmetic. e.g. {GL_ONE, GL_ONE}, {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}.
  292. * @js NA
  293. * @lua NA
  294. */
  295. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  296. /**
  297. * Returns the blending function that is currently being used.
  298. *
  299. * @return A BlendFunc structure with source and destination factor which specified pixel arithmetic.
  300. * @js NA
  301. * @lua NA
  302. */
  303. virtual const BlendFunc &getBlendFunc() const override;
  304. CC_CONSTRUCTOR_ACCESS:
  305. virtual bool init() override;
  306. virtual bool init(const std::string& textContent,
  307. const std::string& fontName,
  308. float fontSize);
  309. protected:
  310. virtual void initRenderer() override;
  311. virtual void onPressStateChangedToNormal() override;
  312. virtual void onPressStateChangedToPressed() override;
  313. virtual void onPressStateChangedToDisabled() override;
  314. virtual void onSizeChanged() override;
  315. void labelScaleChangedWithSize();
  316. virtual Widget* createCloneInstance() override;
  317. virtual void copySpecialProperties(Widget* model) override;
  318. virtual void adaptRenderers() override;
  319. protected:
  320. bool _touchScaleChangeEnabled;
  321. float _normalScaleValueX;
  322. float _normalScaleValueY;
  323. std::string _fontName;
  324. float _fontSize;
  325. float _onSelectedScaleOffset;
  326. Label* _labelRenderer;
  327. bool _labelRendererAdaptDirty;
  328. Type _type;
  329. };
  330. }
  331. NS_CC_END
  332. // end of ui group
  333. /// @}
  334. #endif /* defined(__CocoGUI__Label__) */