1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup UI_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file text_adapter.h 28 * 29 * @brief Defines a text adapter that is used to construct UILabel instances. 30 * 31 * @since 1.0 32 * @version 1.0 33 */ 34 35 #ifndef GRAPHIC_LITE_TEXT_ADAPTER_H 36 #define GRAPHIC_LITE_TEXT_ADAPTER_H 37 38 #include "components/abstract_adapter.h" 39 #include "components/ui_label.h" 40 #include "gfx_utils/list.h" 41 42 namespace OHOS { 43 /** 44 * @brief Defines a text formatter. You can inherit this class and implement the <b>Format()</b> function. 45 * 46 * @since 1.0 47 * @version 1.0 48 */ 49 class TextFormatter : public HeapBase { 50 public: 51 /** 52 * @brief Converts the input integer into a character string for output. 53 * 54 * @param value Indicates the input integer. 55 * @param outText Indicates the pointer to the converted string. 56 * @param textLen Indicates the length of the string. 57 * @since 1.0 58 * @version 1.0 59 */ 60 virtual bool Format(int16_t value, char* outText, uint16_t textLen); 61 }; 62 63 /** 64 * @brief Defines a text adapter, which implements UILabel instance construction and supports text data and continuous 65 * integer data. 66 * 67 * @since 1.0 68 * @version 1.0 69 */ 70 class TextAdapter : public AbstractAdapter { 71 public: 72 /** 73 * @brief A constructor used to create a <b>UILabel</b> instance. 74 * @since 1.0 75 * @version 1.0 76 */ 77 TextAdapter(); 78 79 /** 80 * @brief A destructor used to delete the <b>UILabel</b> instance. 81 * @since 1.0 82 * @version 1.0 83 */ 84 virtual ~TextAdapter(); 85 86 /** 87 * @brief Obtains a <b>UILabel</b> instance to convert adapter data into another <b>UILabel</b> instance. 88 * 89 * @param inView Indicates the pointer to the reusable instance. If this parameter is not <b>NULL</b>, a reusable 90 * <b>UILabel</b> instance is available. In this case, this function does not need to create a new 91 * <b>UILabel</b> instance, just resusing the instance specified by <b>inView</b> to update the <b>inView</b> 92 * data. If this parameter is <b>NULL</b>, there is no resuable <b>UIView</b> instance. In this case, this 93 * function needs to create a new <b>UILabel</b> instance. 94 * 95 * @param index Indicates the adapter data index. 96 * 97 * @return Returns the address of the <b>UILabel</b> instance constructed by the adapter. 98 * @since 1.0 99 * @version 1.0 100 */ 101 UIView* GetView(UIView* inView, int16_t index) override; 102 103 /** 104 * @brief Sets the UILabel adapter data, which is a string linked list. 105 * 106 * @param data Indicates the string linked list data. 107 * @since 1.0 108 * @version 1.0 109 */ 110 void SetData(List<const char*>* data); 111 112 /** 113 * @brief Sets continuously increasing data. For example, if you need to set data <b>0</b>, <b>1</b>, <b>2</b>, 114 * <b>3</b>, <b>4</b>, use this function to set <b>start</b> to 0 and <b>end</b> to 4. 115 * 116 * @param start Indicates the start value of the integer data. 117 * @param end Indicates the end value of the integer data. 118 * @since 1.0 119 * @version 1.0 120 */ 121 void SetData(int16_t start, int16_t end); 122 123 /** 124 * @brief Sets font ID. 125 * 126 * @param fontId Indicates the font ID. For details, see {@link UITextLanguageFontId}. 127 * @since 1.0 128 * @version 1.0 129 */ SetFontId(uint16_t fontId)130 void SetFontId(uint16_t fontId) 131 { 132 fontId_ = fontId; 133 if (fontName_ != nullptr) { 134 UIFree(fontName_); 135 fontName_ = nullptr; 136 } 137 } 138 139 /** 140 * @brief Obtains font ID. 141 * 142 * @return Returns the font ID. For details, see {@link UITextLanguageFontId}. 143 * @since 1.0 144 * @version 1.0 145 */ GetFontId()146 uint16_t GetFontId() const 147 { 148 return fontId_; 149 } 150 151 /** 152 * @brief Sets the font. 153 * 154 * @param name Indicates the pointer to the font name. 155 * @param size Indicates the font size to set. 156 * @since 1.0 157 * @version 1.0 158 */ 159 void SetFont(const char* name, uint8_t size); 160 161 /** 162 * @brief Obtains the UILabel adapter style. 163 * 164 * @return Returns the UILabel adapter style. 165 * @since 1.0 166 * @version 1.0 167 */ GetStyle()168 Style& GetStyle() 169 { 170 return style_; 171 } 172 173 /** 174 * @brief Obtains the <b>data</b> size of the UILabel adapter. 175 * 176 * @return Returns the <b>data</b> size of the UILabel adapter. 177 * @since 1.0 178 * @version 1.0 179 */ 180 uint16_t GetCount() override; 181 182 /** 183 * @brief Sets the width of UILabel constructed by the adapter. 184 * 185 * @return Returns the width of UILabel. 186 * @since 1.0 187 * @version 1.0 188 */ SetWidth(int16_t width)189 void SetWidth(int16_t width) 190 { 191 width_ = width; 192 } 193 194 /** 195 * @brief Sets the direction of the UILabel constructed by the adapter. 196 * 197 * @param direct Indicates the direction of the UILabel constructed by the adapter. 198 * @since 1.0 199 * @version 1.0 200 */ SetDirect(UITextLanguageDirect direct)201 void SetDirect(UITextLanguageDirect direct) 202 { 203 direct_ = direct; 204 } 205 206 /** 207 * @brief Sets the height of the UILabel constructed by the adapter. 208 * 209 * @return Returns the height of the UILabel constructed by the adapter. 210 * @since 1.0 211 * @version 1.0 212 */ SetHeight(int16_t height)213 void SetHeight(int16_t height) 214 { 215 height_ = height; 216 } 217 218 /** 219 * @brief Sets the <b>LineBreakMode</b> attribute of UILabel constructed by the adapter. For details about the 220 * values of <b>LineBreakMode</b>, see {@link LINE_BREAK_ADAPT}, {@link LINE_BREAK_WRAP}, 221 * {@link LINE_BREAK_ELLIPSIS}, and {@link LINE_BREAK_MARQUEE}. 222 * 223 * @param lineBreakMode Indicates the <b>LineBreakMode</b> of UILabel. 224 * @since 1.0 225 * @version 1.0 226 */ SetLineBreakMode(const uint8_t lineBreakMode)227 void SetLineBreakMode(const uint8_t lineBreakMode) 228 { 229 lineBreakMode_ = lineBreakMode; 230 } 231 232 /** 233 * @brief Sets the callback function to be invoked upon a click event. 234 * 235 * @param clickListener Indicates the pointer to the callback function. 236 * @since 1.0 237 * @version 1.0 238 */ SetOnClickListener(UIView::OnClickListener * clickListener)239 void SetOnClickListener(UIView::OnClickListener* clickListener) 240 { 241 clickListener_ = clickListener; 242 } 243 244 /** 245 * @brief Sets the text formatter. 246 * 247 * @param formatter Indicates the pointer to the text formatter. For details, see {@link TextFormatter}. 248 * 249 * @since 1.0 250 * @version 1.0 251 */ SetTextFormatter(TextFormatter * formatter)252 void SetTextFormatter(TextFormatter* formatter) 253 { 254 formatter_ = formatter; 255 } 256 257 protected: 258 virtual UILabel* GetTextView(UIView* inView, int16_t index); 259 virtual UILabel* CreateUILabel(UIView* inView); 260 uint8_t dataMode_; 261 262 private: 263 static constexpr uint8_t DYNAMIC_TEXT_MODE = 0; 264 static constexpr uint8_t CONTINUOUS_INTEGER_MODE = 3; 265 static constexpr uint8_t BUF_LEN = 7; 266 UILabel* GetDynamicText(UIView* inView, int16_t index); 267 UILabel* GetIntegerText(UIView* inView, int16_t index); 268 void ClearDynamicText(); 269 uint16_t fontId_; 270 char* fontName_; 271 uint8_t fontSize_; 272 int16_t width_; 273 int16_t height_; 274 UITextLanguageDirect direct_; 275 uint8_t lineBreakMode_; 276 int16_t integerTextStart_; 277 int16_t integerTextEnd_; 278 Style style_; 279 List<const char*> dynamicText_; 280 UIView::OnClickListener* clickListener_; 281 TextFormatter* formatter_; 282 }; 283 } // namespace OHOS 284 #endif // GRAPHIC_LITE_TEXT_ADAPTER_H 285