• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ui_test_label.h"
17 #include "common/screen.h"
18 #include "components/ui_checkbox.h"
19 #include "components/ui_label.h"
20 #include "font/ui_font.h"
21 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
22 #include "common/spannable_string.h"
23 #endif
24 namespace OHOS {
SetUp()25 void UITestLabel::SetUp()
26 {
27     if (container_ == nullptr) {
28         container_ = new UIScrollView();
29         container_->SetThrowDrag(true);
30         container_->SetHorizontalScrollState(false);
31         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
32         positionX_ = 0;
33         positionY_ = 0;
34     }
35 }
36 
TearDown()37 void UITestLabel::TearDown()
38 {
39     DeleteChildren(container_);
40     container_ = nullptr;
41     uiLabel = nullptr;
42     labelFontSizeBtn1_ = nullptr;
43     labelFontSizeBtn2_ = nullptr;
44     labelFontSizeBtn3_ = nullptr;
45     labelHorAlignBtn1_ = nullptr;
46     labelHorAlignBtn2_ = nullptr;
47     labelHorAlignBtn3_ = nullptr;
48     labelColorBtn1_ = nullptr;
49     labelColorBtn2_ = nullptr;
50     labelColorBtn3_ = nullptr;
51     labelBeyondBtn1_ = nullptr;
52     labelBeyondBtn2_ = nullptr;
53     labelBeyondBtn3_ = nullptr;
54     labelLineBtn1_ = nullptr;
55     labelLineBtn2_ = nullptr;
56     labelVerAlignlBtn1_ = nullptr;
57     labelVerAlignlBtn2_ = nullptr;
58     labelVerAlignlBtn3_ = nullptr;
59     labelDirectionBtn1_ = nullptr;
60     labelDirectionBtn2_ = nullptr;
61     labelSizeBtn1_ = nullptr;
62     labelSizeBtn2_ = nullptr;
63     labelLineHeightBtn1_ = nullptr;
64     labelLineHeightBtn2_ = nullptr;
65     labelLineHeightBtn3_ = nullptr;
66 }
67 
GetTestView()68 const UIView* UITestLabel::GetTestView()
69 {
70     UIKitUILabelTestDisplay001();
71     UIKitUILabeTestDisplay002();
72     UIKitUILabeTestDisplay003();
73     return container_;
74 }
75 
UIKitUILabelTestDisplay001()76 void UITestLabel::UIKitUILabelTestDisplay001()
77 {
78     if (container_ != nullptr) {
79         UIViewGroup* uiViewGroup = new UIViewGroup();
80         // 320: width; 390: height
81         uiViewGroup->SetPosition(positionX_, positionY_, 320, 390);
82         container_->Add(uiViewGroup);
83         UILabel* label = new UILabel();
84         uiViewGroup->Add(label);
85         // 288: width; 48: height
86         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
87         label->SetText("UILabel效果");
88         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
89         UIViewGroup* uiViewGroupFrame = new UIViewGroup();
90         uiViewGroup->Add(uiViewGroupFrame);
91         // 288: width; 336: height
92         uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
93         uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
94         uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
95         uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
96         uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
97         uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
98         uiLabel = new UILabel();
99         uiLabel->SetText("图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统");
100         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
101         uiLabel->SetPosition(0, 141, 280, 336);             // 141: y-coordinate; 280: width; 336: height
102         uiViewGroupFrame->Add(uiLabel);
103         positionX_ += 336; // 336: x-coordinate
104 
105         UIViewGroup* uiViewGroup2 = new UIViewGroup();
106         uiViewGroup2->SetPosition(positionX_, 0, 300, 384); // 300: width; 384: height
107         container_->Add(uiViewGroup2);
108         CreateLabels(uiViewGroup2);
109     }
110 }
111 
CreateLabels(UIViewGroup * uiViewGroup)112 void UITestLabel::CreateLabels(UIViewGroup* uiViewGroup)
113 {
114     if (uiViewGroup == nullptr) {
115         return;
116     }
117     UILabel* label1 = new UILabel();
118     uiViewGroup->Add(label1);
119     // 288: width; 48: height
120     label1->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
121     label1->SetText("字号");
122     label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
123     labelFontSizeBtn1_ = SetUpButton("18 ", 24, 48, uiViewGroup); // 24: x-coordinate; 48: y-coordinate
124     labelFontSizeBtn2_ = SetUpButton("26", 116, 48, uiViewGroup); // 116: x-coordinate; 48: y-coordinate
125     labelFontSizeBtn3_ = SetUpButton("30", 208, 48, uiViewGroup); // 208: x-coordinate; 48: y-coordinate
126 
127     UILabel* label2 = new UILabel();
128     uiViewGroup->Add(label2);
129     label2->SetPosition(24, 107, 288, 48); // 24: x-coordinate, 107: y-coordinate, 288: width; 48: height
130     label2->SetText("水平对齐方式");
131     label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
132     labelHorAlignBtn1_ = SetUpButton("左对齐 ", 24, 144, uiViewGroup);  // 24: x-coordinate; 144: y-coordinate
133     labelHorAlignBtn2_ = SetUpButton("中对齐 ", 116, 144, uiViewGroup); // 116: x-coordinate; 144: y-coordinate
134     labelHorAlignBtn3_ = SetUpButton("右对齐 ", 208, 144, uiViewGroup); // 208: x-coordinate; 144: y-coordinate
135 
136     UILabel* label3 = new UILabel();
137     uiViewGroup->Add(label3);
138     label3->SetPosition(24, 203, 288, 48); // 24: x-coordinate, 203: y-coordinate, 288: width; 48: height
139     label3->SetText("颜色");
140     label3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
141     labelColorBtn1_ = SetUpButton("白色 ", 24, 240, uiViewGroup);    // 24: x-coordinate; 240: y-coordinate
142     labelColorBtn2_ = SetUpButton("变色 ", 116, 240, uiViewGroup);   // 116: x-coordinate; 240: y-coordinate
143     labelColorBtn3_ = SetUpButton("半透明 ", 208, 240, uiViewGroup); // 208: x-coordinate; 240: y-coordinate
144 
145     UILabel* label4 = new UILabel();
146     uiViewGroup->Add(label4);
147     label4->SetPosition(24, 299, 288, 48); // 24: x-coordinate, 299: y-coordinate, 288: width; 48: height
148     label4->SetText("超出处理");
149     label4->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
150     labelBeyondBtn1_ = SetUpButton("截断 ", 24, 336, uiViewGroup);  // 24: x-coordinate; 336: y-coordinate
151     labelBeyondBtn2_ = SetUpButton("省略 ", 116, 336, uiViewGroup); // 116: x-coordinate; 336: y-coordinate
152     labelBeyondBtn3_ = SetUpButton("滚动 ", 208, 336, uiViewGroup); // 208: x-coordinate; 336: y-coordinate
153     positionX_ += 312;                                              // 312: increase y-coordinate
154 }
155 
UIKitUILabeTestDisplay002()156 void UITestLabel::UIKitUILabeTestDisplay002()
157 {
158     if (container_ != nullptr) {
159         UIViewGroup* uiViewGroup = new UIViewGroup();
160         uiViewGroup->SetPosition(positionX_, positionY_, 320, 390); // 320: width; 390: height
161         container_->Add(uiViewGroup);
162         UILabel* label = new UILabel();
163         uiViewGroup->Add(label);
164         // 288: width; 48: height
165         label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
166         label->SetText("行数");
167         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
168         labelLineBtn1_ = SetUpButton("单行 ", 24, 48, uiViewGroup);  // 24: x-coordinate; 48: y-coordinate
169         labelLineBtn2_ = SetUpButton("多行 ", 116, 48, uiViewGroup); // 116: x-coordinate; 48: y-coordinate
170         UILabel* label2 = new UILabel();
171         uiViewGroup->Add(label2);
172         label2->SetPosition(24, 107, 288, 48); // 24: x-coordinate, 107: y-coordinate, 288: width; 48: height
173         label2->SetText("竖直对齐方式");
174         label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
175         labelVerAlignlBtn1_ = SetUpButton("上对齐 ", 24, 144, uiViewGroup);  // 24: x-coordinate; 144: y-coordinate
176         labelVerAlignlBtn2_ = SetUpButton("中对齐 ", 116, 144, uiViewGroup); // 116: x-coordinate; 144: y-coordinate
177         labelVerAlignlBtn3_ = SetUpButton("下对齐 ", 208, 144, uiViewGroup); // 208: x-coordinate; 144: y-coordinate
178         UILabel* label3 = new UILabel();
179         uiViewGroup->Add(label3);
180         label3->SetPosition(24, 203, 288, 48); // 24: x-coordinate, 203: y-coordinate, 288: width; 48: height
181         label3->SetText("方向");
182         label3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
183         labelDirectionBtn1_ = SetUpButton("左往右 ", 24, 240, uiViewGroup);  // 24: x-coordinate; 240: y-coordinate
184         labelDirectionBtn2_ = SetUpButton("右往左 ", 116, 240, uiViewGroup); // 116: x-coordinate; 240: y-coordinate
185         UILabel* label4 = new UILabel();
186         uiViewGroup->Add(label4);
187         label4->SetPosition(24, 299, 288, 48); // 24: x-coordinate, 299: y-coordinate, 288: width; 48: height
188         label4->SetText("行高");
189         label4->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
190         labelLineHeightBtn1_ = SetUpButton("行高20 ", 24, 336, uiViewGroup);  // 24: x-coordinate; 336: y-coordinate
191         labelLineHeightBtn2_ = SetUpButton("行高35 ", 116, 336, uiViewGroup); // 116: x-coordinate; 336: y-coordinate
192         labelLineHeightBtn3_ = SetUpButton("行高50 ", 208, 336, uiViewGroup); // 208: x-coordinate; 336: y-coordinate
193         positionY_ += 384;                                                   // 384: increase x-coordinate
194         positionX_ -= 312;                                                   // 312: increase y-coordinate
195     }
196 }
197 
UIKitUILabeTestDisplay003()198 void UITestLabel::UIKitUILabeTestDisplay003()
199 {
200     if (container_ != nullptr) {
201         UIViewGroup* uiViewGroup = new UIViewGroup();
202         uiViewGroup->SetPosition(positionX_, positionY_, 450, 110); // 450: width; 110: height
203         container_->Add(uiViewGroup);
204         UILabel* label = new UILabel();
205         uiViewGroup->Add(label);
206         label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 420, 48); // 420: width; 48: height
207         label->SetText("Label大小");
208         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
209         // 24: x-coordinate; 48: y-coordinate; 120: width
210         labelSizeBtn1_ = SetUpButton("动态宽度 ", 24, 48, uiViewGroup, 120);
211         // 156: x-coordinate; 48: y-coordinate; 120: width
212         labelSizeBtn2_ = SetUpButton("动态高度 ", 156, 48, uiViewGroup, 120);
213     }
214 }
215 
OnClick(UIView & view,const ClickEvent & event)216 bool UITestLabel::OnClick(UIView& view, const ClickEvent& event)
217 {
218     uiLabel->Resize(288, 100);                          // 288: width; 100: height
219     uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
220     if (&view == labelFontSizeBtn1_) {
221         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18); // 18: font size
222     } else if (&view == labelFontSizeBtn2_) {
223         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
224     } else if (&view == labelFontSizeBtn3_) {
225         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
226     } else if (&view == labelHorAlignBtn1_) {
227         uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
228     } else if (&view == labelHorAlignBtn2_) {
229         uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
230     } else if (&view == labelHorAlignBtn3_) {
231         uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
232     } else if (&view == labelColorBtn1_) {
233         uiLabel->SetStyle(STYLE_TEXT_COLOR, Color::White().full);
234         uiLabel->SetStyle(STYLE_TEXT_OPA, OPA_OPAQUE);
235     } else if (&view == labelColorBtn2_) {
236         uiLabel->SetStyle(STYLE_TEXT_COLOR, Color::Red().full);
237         uiLabel->SetStyle(STYLE_TEXT_OPA, OPA_OPAQUE);
238     } else if (&view == labelColorBtn3_) {
239         uiLabel->SetStyle(STYLE_TEXT_OPA, 127); // 127: opaque
240     } else if (&view == labelBeyondBtn1_) {
241         uiLabel->Resize(288, 35); // 288: width; 35: height
242         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
243         uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
244         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
245     } else if (&view == labelBeyondBtn2_) {
246         uiLabel->Resize(288, 35); // 288: width; 35: height
247         uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
248         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
249     } else if (&view == labelBeyondBtn3_) {
250         uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
251         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
252     } else {
253         ExpandClick(view, event);
254     }
255     return true;
256 }
257 
ExpandClick(UIView & view,const ClickEvent & event) const258 void UITestLabel::ExpandClick(UIView& view, const ClickEvent& event) const
259 {
260     if (&view == labelLineBtn1_) {
261         uiLabel->Resize(288, 50);                           // 288: width; 50: height
262         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30:font size
263         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
264     } else if (&view == labelLineBtn2_) {
265         uiLabel->Resize(288, 100);                          // 288: width; 100: height
266         uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30:font size
267         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
268     } else if (&view == labelVerAlignlBtn1_) {
269         uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_TOP);
270     } else if (&view == labelVerAlignlBtn2_) {
271         uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
272     } else if (&view == labelVerAlignlBtn3_) {
273         uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_BOTTOM);
274     } else if (&view == labelDirectionBtn1_) {
275         uiLabel->SetDirect(TEXT_DIRECT_LTR);
276     } else if (&view == labelDirectionBtn2_) {
277         uiLabel->SetDirect(TEXT_DIRECT_RTL);
278     } else if (&view == labelSizeBtn1_) {
279         uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
280         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
281     } else if (&view == labelSizeBtn2_) {
282         uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
283         uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
284     } else if (&view == labelLineHeightBtn1_) {
285         uiLabel->SetStyle(STYLE_LINE_HEIGHT, 20); // 20: lineHeight
286     } else if (&view == labelLineHeightBtn2_) {
287         uiLabel->SetStyle(STYLE_LINE_HEIGHT, 35); // 35: lineHeight
288     } else if (&view == labelLineHeightBtn3_) {
289         uiLabel->SetStyle(STYLE_LINE_HEIGHT, 50); // 50: lineHeight
290     }
291 }
292 } // namespace OHOS
293