问题现象
在文本量多而杂的场景下,单独给每个Text文本设置字体大小效率不高,有什么方法能够设置APP内的全局字体大小?以便统一设置字体大小,提高效率。
背景知识
动态属性是动态设置组件的属性,支持开发者在属性设置时使用if/else语法,且根据需要使用多态样式设置属性。其属性方法AttributeModifier支持在当前组件上动态设置属性方法。
Text文本组件中的fontSize属性可以设置字体大小,fontFamily属性可以设置字体列表。
接口ApplicationContext.setFontSizeScale设置应用字体大小缩放比例。仅支持主线程调用。当应用字体设置为跟随系统时,通过配置configuration标签就可以设置应用字体大小跟随系统变化的比例。
解决方案
方案一:通过preferences调节应用内全局字体大小。
自定义PreferencesUtil类,通过其提供的创建、保存和查询的数据,将读取到的数据保存到页面带有@State的变量中,通过状态变量对文本字体大小进行设置。具体应用可以参考官网使用preferences实现应用内字体大小调节功能。
方案二:通过动态属性设置全局字体大小。
使用动态属性,自定义class实现AttributeModifier接口,设定好Text需要的fontSize后在页面调用,实现字体大小统一修改。
class MyTextModifier implements AttributeModifier<TextAttribute> {
public isDark: boolean = false;
applyNormalAttribute(instance: TextAttribute): void {
if (this.isDark) {
instance.fontSize(100);
} else {
instance.fontSize(50);
}
}
}
@Entry
@Component
struct Index {
private message: string = 'test';
@State modifier: MyTextModifier = new MyTextModifier();
build() {
Column() {
Text(this.message)
.attributeModifier(this.modifier)
.onClick(() => {
this.modifier.isDark = !this.modifier.isDark;
});
Blank();
Text(this.message)
.attributeModifier(this.modifier)
.onClick(() => {
this.modifier.isDark = !this.modifier.isDark;
});
}
.padding(24)
.width('100%')
.height('100%');
}
}
https://pastebin.com/zv1bEjfh
https://pastebin.com/Z7CHKjjW
https://pastebin.com/73NZB1fg
https://pastebin.com/zb96CYYT
https://pastebin.com/TVSbCuk0
https://pastebin.com/PQH1YKrR
https://pastebin.com/bv5qqT8j
https://pastebin.com/WpenkS9J
https://pastebin.com/jkgzXhCX
https://pastebin.com/pT7xkBGP
https://pastebin.com/ppyQuy1c
https://pastebin.com/LdT3rmMu
https://pastebin.com/EVTt0G5t
https://pastebin.com/Lv4a1muV
https://pastebin.com/zmTAWL2U
https://pastebin.com/QdxecLbK
https://pastebin.com/3kZ5NwrU
https://pastebin.com/TPJsBatd
https://pastebin.com/WfhnNpFV
https://pastebin.com/SAmdt79t
https://pastebin.com/JZ4KvEWL
https://pastebin.com/uGcyWALB
https://pastebin.com/MEq4JbqR
https://pastebin.com/3ucqcbc6
https://pastebin.com/3cDtaAwF
https://pastebin.com/yzn2GGYf
https://pastebin.com/RTjPyJNX
https://pastebin.com/SQDC9M1E
https://pastebin.com/xcasG7xf
https://pastebin.com/zZfPyQY3
https://pastebin.com/RNzWpA4q
https://pastebin.com/8pufepjf
https://pastebin.com/vxDSuXem
https://pastebin.com/HP4y0h1f
https://pastebin.com/46jYNt7j
https://pastebin.com/YUvcKeAw
https://pastebin.com/wGfKDBZe
https://pastebin.com/bccZqgn7
https://pastebin.com/cBisnNN3
https://pastebin.com/4WFqkrmi
https://pastebin.com/fc7jp78E
https://pastebin.com/yjptCxY9
https://pastebin.com/2D6R3N5s
https://pastebin.com/KvR4cDki
https://pastebin.com/Vk1Rk7uj
https://pastebin.com/JEiNNYpi
https://pastebin.com/PyzV38G0
https://pastebin.com/KbzwD20r
https://pastebin.com/ViKS1JgQ
https://pastebin.com/5YcYwqDE
https://pastebin.com/ZFMU4ndk
https://pastebin.com/0Ex2wTma
https://pastebin.com/pQ7MD6DZ
https://pastebin.com/UCTHHnqH
https://pastebin.com/sDEyLgsy
https://pastebin.com/Z7Wrv3qq
https://pastebin.com/qb5YncFh
https://pastebin.com/uibzH23E
https://pastebin.com/4ykQaXqk
https://pastebin.com/K5mKVf1G
https://pastebin.com/xYEugHiP
https://pastebin.com/z8JzmxYY
https://pastebin.com/0UXR57Di
https://pastebin.com/0hmA61Hd
https://pastebin.com/tPkKjDC1
https://pastebin.com/v0ra9Mya
https://pastebin.com/puHjxuHP
https://pastebin.com/fYS5Dnw5
https://pastebin.com/xDwmcBVp
https://pastebin.com/vYi6rTmN
https://pastebin.com/yx39A9W3
https://pastebin.com/YjGpgumr
https://pastebin.com/Yr3NDGsw
https://pastebin.com/mHQDdmYW
https://pastebin.com/zQj53Zez
https://pastebin.com/BPjewhxX
https://pastebin.com/zQ0vrSFY
https://pastebin.com/1w62VYp5
https://pastebin.com/h4KEm2mW
https://pastebin.com/fTskMDFx
https://pastebin.com/FmNtGTnb
https://pastebin.com/16dZdmr6
https://pastebin.com/GDKtKnBU
https://pastebin.com/s1CTn9wk
https://pastebin.com/HWayJh7D
https://pastebin.com/sbNvhpjd
https://pastebin.com/8QS7hR6z
https://pastebin.com/BNmr0siT
https://pastebin.com/wSeQ93jQ
https://pastebin.com/mWDHpM97
https://pastebin.com/n95D4DU9
https://pastebin.com/WwyN4MSy
https://pastebin.com/Yy5piny4
https://pastebin.com/M5eG7d97
https://pastebin.com/tecUdChP
https://pastebin.com/YizPCgVe
https://pastebin.com/k2QgxpVx
https://pastebin.com/L874Auw2
https://pastebin.com/i7xaQUHV
https://pastebin.com/6RYNSaMD
https://pastebin.com/EHGryvUF
https://pastebin.com/mBy1dH2W
https://pastebin.com/4Rv7wPLE
https://pastebin.com/
https://pastebin.com/sc89ynXn
https://pastebin.com/BAmNnWPG
https://pastebin.com/KgFLHfXf
https://pastebin.com/LT7WpAkz
https://pastebin.com/F7bhcEZn
https://pastebin.com/Vt71iUie
https://pastebin.com/b6aTG0vH
https://pastebin.com/YkTNEfNW
https://pastebin.com/xZYNrFV9
https://pastebin.com/URiZmFBE
https://pastebin.com/31PwZST4
https://pastebin.com/9zzUgrh1
https://pastebin.com/t5GZ46L5
https://pastebin.com/jnqax73J
https://pastebin.com/s8ancsQ8
https://pastebin.com/RupTptFa
https://pastebin.com/iqVL98RE
https://pastebin.com/m540xPh3
https://pastebin.com/SGt4MpRD
https://pastebin.com/d4CT63kJ
https://pastebin.com/hhPTDEMT
https://pastebin.com/RHTCf9E2
https://pastebin.com/0T6WBFQs
https://pastebin.com/i1UChYJG
https://pastebin.com/m5EmezEN
https://pastebin.com/7da2hn5E
https://pastebin.com/RCSThm9K
https://pastebin.com/HMwehjXj
https://pastebin.com/5rYR3q2t
https://pastebin.com/2hBwYzin
https://pastebin.com/p7QyQegF
https://pastebin.com/9YchUWAp
https://pastebin.com/9QkWjZS8
https://pastebin.com/dKJcBeWU
https://pastebin.com/y2hUEn5k
https://pastebin.com/PjRCfHQV
https://pastebin.com/Zwvh6Gj6
https://pastebin.com/QQYxQzn7
https://pastebin.com/9TPRM0EL
https://pastebin.com/mAX55z1X
https://pastebin.com/NSCC7keU
https://pastebin.com/zxpEz8CT
https://pastebin.com/N4qU4GBi
https://pastebin.com/fMsMLean
https://pastebin.com/Xgq14P8Z
https://pastebin.com/zD7r5kSj
https://pastebin.com/MA8wHsQp
https://pastebin.com/Ww5Sedpx
https://pastebin.com/MH5DNyY1
https://pastebin.com/A8ZR80ix
https://pastebin.com/An6A0kcB
https://pastebin.com/xfZyEP3r
https://pastebin.com/mCRJJG5k
https://pastebin.com/i8pDjjTr
https://pastebin.com/x5EFGiqg
https://pastebin.com/hdGJGHDa
https://pastebin.com/XvLmLTda
https://pastebin.com/URSmHfMQ
https://pastebin.com/AfL6JvxE
https://pastebin.com/2XwNG2Gv
https://pastebin.com/yCggCwQs
https://pastebin.com/vtfpaBVx
https://pastebin.com/e5QbtBdu
https://pastebin.com/xBW2j2u7
https://pastebin.com/EM9mfHFE
https://pastebin.com/YdSsZfrd
https://pastebin.com/29t7hwPk
https://pastebin.com/AzL6dAAr
https://pastebin.com/9DjHEwu4
https://pastebin.com/zsG1FAQ5
https://pastebin.com/n3u3F4FS
https://pastebin.com/hGb4579u
https://pastebin.com/4iVXD2bm
https://pastebin.com/asamK1S5
https://pastebin.com/7bjibNp4
https://pastebin.com/KJaFD1nA
https://pastebin.com/CcD19MmF
https://pastebin.com/rFBHrupt
https://pastebin.com/Kf4zzEZ1
https://pastebin.com/p07Rr3Dg
https://pastebin.com/TzBYBTKd
https://pastebin.com/3mSdSwZc
https://pastebin.com/vD58SSEh
https://pastebin.com/yL5bYn4f
https://pastebin.com/NfsJ1BJa
https://pastebin.com/Dmf05JZi
https://pastebin.com/ZNAR7d19
https://pastebin.com/ni3NfQvW
https://pastebin.com/G7gt0EP1
https://pastebin.com/wej3w4Vi
https://pastebin.com/d602uf7W
https://pastebin.com/Y43ufxnM
https://pastebin.com/iCT3zSXx
https://pastebin.com/T3jA1SUw
https://pastebin.com/n4U1b4jm
1 个评论
要回复文章请先登录或注册
用户3096723 (作者)