目的
实现跨文件设置标签样式
封装Modifier
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import CommonConstants from '../common/constants/CommonConstants' export enum LauncherAttrType { BACKGROUND, LOGO, COLUMN, TEXT, INTRODUCE } export interface LauncherModifier { background: LauncherAttrModifier<ImageAttribute> logo: LauncherAttrModifier<ImageAttribute> column: LauncherAttrModifier<ColumnAttribute> text: LauncherAttrModifier<TextAttribute> introduce: LauncherAttrModifier<TextAttribute> } export const launcherModifier = (): LauncherModifier => { return { background: new LauncherAttrModifier(LauncherAttrType.BACKGROUND), logo: new LauncherAttrModifier(LauncherAttrType.LOGO), column: new LauncherAttrModifier(LauncherAttrType.COLUMN), text: new LauncherAttrModifier(LauncherAttrType.TEXT), introduce: new LauncherAttrModifier(LauncherAttrType.INTRODUCE) } } export class LauncherAttrModifier<Attribute extends CommonAttribute | TextAttribute> implements AttributeModifier<Attribute> { attrType: LauncherAttrType constructor(attrType: LauncherAttrType) { this.attrType = attrType } applyNormalAttribute(instance: Attribute): void { this.setBgStyle(instance) this.setLogoStyle(instance) this.setColumnStyle(instance) this.setTextStyle(instance as TextAttribute) this.setIntroduceStyle(instance as TextAttribute) } setBgStyle(instance: Attribute) { (this.attrType === LauncherAttrType.BACKGROUND) && instance .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_HEIGHT) } setLogoStyle(instance: Attribute) { (this.attrType === LauncherAttrType.LOGO) && instance .width($r('app.float.launcher_logo_size')) .height($r('app.float.launcher_logo_size')) .margin({ top: CommonConstants.LAUNCHER_IMAGE_MARGIN_TOP }) } setColumnStyle(instance: Attribute) { (this.attrType === LauncherAttrType.COLUMN) && instance .width(CommonConstants.FULL_WIDTH) .height(CommonConstants.FULL_HEIGHT) } setTextStyle(instance: TextAttribute) { (this.attrType === LauncherAttrType.TEXT) && instance .width($r('app.float.launcher_life_text_width')) .height($r('app.float.launcher_life_text_height')) .fontWeight(FontWeight.Bold) .letterSpacing(CommonConstants.LAUNCHER_LIFE_TEXT_SPACING) .fontSize($r('app.float.launcher_text_title_size')) .fontColor($r('app.color.launcher_text_title_color')) .margin({ top: CommonConstants.LAUNCHER_TEXT_TITLE_MARGIN_TOP }) } setIntroduceStyle(instance: TextAttribute) { (this.attrType === LauncherAttrType.INTRODUCE) && instance .height(CommonConstants.LAUNCHER_TEXT_INTRODUCE_HEIGHT) .fontWeight(FontWeight.Normal) .letterSpacing(CommonConstants.LAUNCHER_TEXT_INTRODUCE_SPACING) .fontSize($r('app.float.launcher_text_introduce_size')) .fontColor($r('app.color.launcher_text_introduce_color')) .opacity($r('app.float.launcher_text_opacity')) .margin({ top: CommonConstants.LAUNCHER_TEXT_INTRODUCE_MARGIN_TOP }) } } |
在页面使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import { launcherModifier, LauncherModifier } from '../attributemodifier/LauncherModifier' @Entry @Component struct LauncherPage { private modifier: LauncherModifier = launcherModifier() build() { Stack() { Image($r('app.media.ic_launcher_background')) .attributeModifier(this.modifier.background) Column() { Image($r('app.media.ic_logo')) .attributeModifier(this.modifier.logo) Text($r('app.string.healthy_life_text')) .attributeModifier(this.modifier.text) Text($r('app.string.healthy_life_introduce')) .attributeModifier(this.modifier.introduce) } .attributeModifier(this.modifier.column) } } } |