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