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 ui_label.h 28 * 29 * @brief Declares a <b>UILabel</b> class that represents a label. 30 * 31 * @since 1.0 32 * @version 1.0 33 */ 34 35 #ifndef GRAPHIC_LITE_UI_LABEL_H 36 #define GRAPHIC_LITE_UI_LABEL_H 37 38 #include "animator/animator.h" 39 #include "common/text.h" 40 #include "components/ui_view.h" 41 42 namespace OHOS { 43 /** 44 * @brief Defines the functions for presenting a label in a specified area, setting the style and background color 45 * of a label, and setting the display mode of a long label text. 46 * 47 * @since 1.0 48 * @version 1.0 49 */ 50 class UILabel : public UIView { 51 public: 52 /** 53 * @brief Enumerates the display modes of a long text. 54 */ 55 enum LineBreakMode : uint8_t { 56 /** 57 * The label size is adaptive to the text size. 58 */ 59 LINE_BREAK_ADAPT = 0, 60 /** 61 * The height of this label remains unchanged, and the width is adaptive to the text size. 62 */ 63 LINE_BREAK_STRETCH, 64 /** 65 * The width of this label remains unchanged, and the height is adaptive to the text size. 66 * The text switches to the next line if the text exceeds the maximum label width. 67 */ 68 LINE_BREAK_WRAP, 69 /** 70 * The width and height of this label remain unchanged. 71 * If this text is too long, ellipsis will be used at the end. 72 */ 73 LINE_BREAK_ELLIPSIS, 74 /** 75 * The width and height of this label remain unchanged. 76 * If this text is too long, it will be rolled to display. 77 */ 78 LINE_BREAK_MARQUEE, 79 /** 80 * The width and height of this label remain unchanged. 81 * If this text is too long, it will be cropped to display. 82 */ 83 LINE_BREAK_CLIP, 84 /** 85 * Maximum value of the line break mode, which is used for validity check. 86 */ 87 LINE_BREAK_MAX, 88 }; 89 90 /** 91 * @brief A constructor used to create a <b>UILabel</b> instance. 92 * 93 * @since 1.0 94 * @version 1.0 95 */ 96 UILabel(); 97 98 /** 99 * @brief A destructor used to delete the <b>UILabel</b> instance. 100 * 101 * @since 1.0 102 * @version 1.0 103 */ 104 virtual ~UILabel(); 105 106 /** 107 * @brief Obtains the view type. 108 * 109 * @return Returns <b>UI_LABEL</b>, as defined in {@link UIViewType}. 110 * @since 1.0 111 * @version 1.0 112 */ GetViewType()113 UIViewType GetViewType() const override 114 { 115 return UI_LABEL; 116 } 117 118 /** 119 * @brief Obtains the width of this label. 120 * 121 * @return Returns the label width. 122 * @since 1.0 123 * @version 1.0 124 */ 125 int16_t GetWidth() override; 126 127 /** 128 * @brief Obtains the height of this label. 129 * 130 * @return Returns the label height. 131 * @since 1.0 132 * @version 1.0 133 */ 134 int16_t GetHeight() override; 135 136 /** 137 * @brief Sets the view style. 138 * @param style Indicates the view style. 139 * @since 1.0 140 * @version 1.0 141 */ SetStyle(Style & style)142 void SetStyle(Style& style) override 143 { 144 UIView::SetStyle(style); 145 } 146 147 /** 148 * @brief Sets a style. 149 * 150 * @param key Indicates the key of the style to set. 151 * @param value Indicates the value matching the key. 152 * @since 1.0 153 * @version 1.0 154 */ 155 void SetStyle(uint8_t key, int64_t value) override; 156 157 /** 158 * @brief Checks whether this label needs to be covered before drawing it. 159 * 160 * @param invalidatedArea Indicates the area to draw. 161 * @return Returns <b>true</b> if this label needs to be covered; returns <b> false</b> otherwise. 162 * @since 1.0 163 * @version 1.0 164 */ OnPreDraw(Rect & invalidatedArea)165 bool OnPreDraw(Rect& invalidatedArea) const override 166 { 167 return false; 168 } 169 170 /** 171 * @brief Draws this label. 172 * 173 * @param invalidatedArea Indicates the area to draw. 174 * @since 1.0 175 * @version 1.0 176 */ 177 void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 178 179 /** 180 * @brief Sets the text content for this label. 181 * 182 * @param text Indicates the pointer to the text content. 183 * @since 1.0 184 * @version 1.0 185 */ 186 void SetText(const char* text); 187 188 /** 189 * @brief Obtains the text of this label. 190 * 191 * @return Returns the text. 192 * @since 1.0 193 * @version 1.0 194 */ GetText()195 const char* GetText() const 196 { 197 return (labelText_ == nullptr) ? nullptr : labelText_->GetText(); 198 } 199 200 /** 201 * @brief Sets the line break mode for this text. 202 * 203 * @param lineBreakMode Indicates the line break mode to set. 204 * @since 1.0 205 * @version 1.0 206 */ 207 void SetLineBreakMode(const uint8_t lineBreakMode); 208 209 /** 210 * @brief Obtains the line break mode of this text. 211 * 212 * @return Returns the line break mode. 213 * @since 1.0 214 * @version 1.0 215 */ GetLineBreakMode()216 uint8_t GetLineBreakMode() const 217 { 218 return lineBreakMode_; 219 } 220 221 /** 222 * @brief Sets the color for this text. 223 * 224 * @param color Indicates the text color to set. 225 * @since 1.0 226 * @version 1.0 227 */ SetTextColor(ColorType color)228 void SetTextColor(ColorType color) 229 { 230 useTextColor_ = true; 231 textColor_ = color; 232 } 233 234 /** 235 * @brief Obtains the color of this text. 236 * 237 * @return Returns the text color. 238 * @since 1.0 239 * @version 1.0 240 */ GetTextColor()241 ColorType GetTextColor() const 242 { 243 return useTextColor_ ? textColor_ : GetStyleConst().textColor_; 244 } 245 246 /** 247 * @brief Sets the alignment mode for this text. 248 * 249 * @param horizontalAlign Indicates the horizontal alignment mode to set, 250 * which can be {@link TEXT_ALIGNMENT_LEFT}, 251 * {@link TEXT_ALIGNMENT_CENTER}, or {@link TEXT_ALIGNMENT_RIGHT}. 252 * @param verticalAlign Indicates the vertical alignment mode to set, which can be 253 * {@link TEXT_ALIGNMENT_TOP} (default mode), {@link TEXT_ALIGNMENT_CENTER}, 254 * or {@link TEXT_ALIGNMENT_BOTTOM}. 255 * @since 1.0 256 * @version 1.0 257 */ 258 void SetAlign(UITextLanguageAlignment horizontalAlign, 259 UITextLanguageAlignment verticalAlign = TEXT_ALIGNMENT_TOP); 260 261 /** 262 * @brief Obtains the horizontal alignment mode. 263 * 264 * @return Returns the horizontal alignment mode. 265 * @since 1.0 266 * @version 1.0 267 */ GetHorAlign()268 UITextLanguageAlignment GetHorAlign() 269 { 270 InitLabelText(); 271 return labelText_->GetHorAlign(); 272 } 273 274 /** 275 * @brief Obtains the vertical alignment mode. 276 * 277 * @return Returns the vertical alignment mode. 278 * @since 1.0 279 * @version 1.0 280 */ GetVerAlign()281 UITextLanguageAlignment GetVerAlign() 282 { 283 InitLabelText(); 284 return labelText_->GetVerAlign(); 285 } 286 287 /** 288 * @brief Sets the direction for this text. 289 * 290 * @return direct Returns the text direction, as defined in {@link UITextLanguageDirect}. 291 * @since 1.0 292 * @version 1.0 293 */ SetDirect(UITextLanguageDirect direct)294 void SetDirect(UITextLanguageDirect direct) 295 { 296 InitLabelText(); 297 labelText_->SetDirect(direct); 298 } 299 300 /** 301 * @brief Obtains the direction of this text. 302 * 303 * @return Returns the text direction, as defined in {@link UITextLanguageDirect}. 304 * @since 1.0 305 * @version 1.0 306 */ GetDirect()307 UITextLanguageDirect GetDirect() 308 { 309 InitLabelText(); 310 return labelText_->GetDirect(); 311 } 312 313 /** 314 * @brief Sets the font ID for this label. 315 * 316 * @param fontId Indicates the font ID composed of font name and size. 317 * @since 1.0 318 * @version 1.0 319 */ 320 void SetFontId(uint8_t fontId); 321 322 /** 323 * @brief Obtains the font ID composed of font name and size. 324 * 325 * @return Returns the front ID of this label. 326 * @since 1.0 327 * @version 1.0 328 */ GetFontId()329 uint8_t GetFontId() 330 { 331 InitLabelText(); 332 return labelText_->GetFontId(); 333 } 334 335 /** 336 * @brief Sets the font for this label. 337 * 338 * @param name Indicates the pointer to the font name. 339 * @param size Indicates the font size to set. 340 * @since 1.0 341 * @version 1.0 342 */ 343 void SetFont(const char* name, uint8_t size); 344 345 /** 346 * @brief Sets the scroll speed for this text. 347 * 348 * @param speed Indicates the scroll speed to set. 349 * @since 1.0 350 * @version 1.0 351 */ 352 void SetRollSpeed(uint16_t speed); 353 354 /** 355 * @brief Obtains the width of this text. 356 * 357 * @return Returns the text width. 358 * @since 1.0 359 * @version 1.0 360 */ 361 uint16_t GetTextWidth(); 362 363 /** 364 * @brief Obtains the height of this text. 365 * 366 * @return Returns the text height. 367 * @since 1.0 368 * @version 1.0 369 */ 370 uint16_t GetTextHeight(); 371 372 /** 373 * @brief Sets the position where this text starts to roll. 374 * 375 * @param pos Indicates the position to set. 376 * @since 1.0 377 * @version 1.0 378 */ 379 void SetRollStartPos(int16_t pos); 380 381 /** 382 * @brief Obtains the position where this text starts to roll. 383 * 384 * @return Returns the position where this text starts to roll. 385 * @since 1.0 386 * @version 1.0 387 */ 388 int16_t GetRollStartPos() const; 389 390 /** 391 * @brief Sets the width for this label. 392 * 393 * @param width Indicates the width to set. 394 * @since 1.0 395 * @version 1.0 396 */ 397 void SetWidth(int16_t width) override; 398 399 /** 400 * @brief Sets the height for this label. 401 * 402 * @param height Indicates the height to set. 403 * @since 1.0 404 * @version 1.0 405 */ 406 void SetHeight(int16_t height) override; 407 408 void ReMeasure() override; 409 SetSupportBaseLine(bool baseLine)410 void SetSupportBaseLine(bool baseLine) 411 { 412 InitLabelText(); 413 labelText_->SetSupportBaseLine(baseLine); 414 } 415 416 protected: 417 Text* labelText_; 418 void RefreshLabel(); 419 420 virtual void InitLabelText(); 421 422 private: 423 friend class LabelAnimator; 424 425 void RemeasureForMarquee(int16_t textWidth); 426 427 bool needRefresh_ : 1; 428 bool useTextColor_ : 1; 429 bool hasAnimator_ : 1; 430 uint8_t lineBreakMode_ : 4; 431 uint16_t ellipsisIndex_; 432 int16_t offsetX_; 433 ColorType textColor_; 434 435 static constexpr uint16_t DEFAULT_ANIMATOR_SPEED = 35; 436 union { 437 Animator* animator; 438 struct { 439 uint16_t speed; 440 int16_t pos; 441 }; 442 } animator_; 443 }; 444 } // namespace OHOS 445 #endif // GRAPHIC_LITE_UI_LABEL_H 446