1 /*
2  * Copyright (c) 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 #include "text_label_adapter.h"
16 #include "language/language_ui.h"
17 #include "log/log.h"
18 #include "updater_ui_const.h"
19 #include "view_api.h"
20 namespace Updater {
TextLabelAdapter(const UxViewInfo & info)21 TextLabelAdapter::TextLabelAdapter(const UxViewInfo &info)
22 {
23     const UxLabelInfo &spec = std::get<UxLabelInfo>(info.specificInfo);
24     SetViewCommonInfo(info.commonInfo);
25     this->SetAlign(GetAlign(spec.align), OHOS::TEXT_ALIGNMENT_CENTER);
26     this->SetText(TranslateText(spec.text).c_str());
27     this->SetFont(DEFAULT_FONT_FILENAME, spec.fontSize);
28     this->SetStyle(OHOS::STYLE_LINE_HEIGHT, UPDATER_UI_FONT_HEIGHT_RATIO * spec.fontSize);
29     auto fontColor = StrToColor(spec.fontColor);
30     this->SetStyle(OHOS::STYLE_TEXT_COLOR, fontColor.full);
31     this->SetStyle(OHOS::STYLE_TEXT_OPA, fontColor.alpha);
32     auto bgColor = StrToColor(spec.bgColor);
33     this->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
34     this->SetStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
35 }
36 
IsValid(const UxLabelInfo & info)37 bool TextLabelAdapter::IsValid(const UxLabelInfo &info)
38 {
39     if (info.fontSize > MAX_FONT_SIZE) {
40         LOG(ERROR) << "label viewinfo check failed, fontSize: " << info.fontSize;
41         return false;
42     }
43 
44     if (!CheckColor(info.bgColor) || !CheckColor(info.fontColor)) {
45         LOG(ERROR) << "label viewinfo check failed, bgColor:" << info.bgColor <<
46             " fontColor:" << info.fontColor;
47         return false;
48     }
49     return true;
50 }
51 
SetText(const std::string & txt)52 void TextLabelAdapter::SetText(const std::string &txt)
53 {
54     // usage of "*" is same as label button's SetText
55     if (txt == "*") {
56         return;
57     }
58     OHOS::UILabel::SetText(txt.c_str());
59 }
60 } // namespace Updater
61