fontScale.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * 小程序字体缩放适配
  3. *
  4. * 字号来源优先级:用户手动设置 > 微信系统字号(fontSizeScaleFactor / fontSizeSetting)> 1(默认)
  5. * 值域钳制在 [0.9, 1.7],超出视为无效值按默认处理。
  6. *
  7. * 平台差异说明(微信体系,基于 uni.getAppBaseInfo()):
  8. * - fontSizeScaleFactor:缩放比例(直接可用)
  9. * - fontSizeSetting:用户设置的字号 px 值(基准 16px),需基础库 2.23.4+
  10. * - rpx 不随字号变化,需手动乘系数才能让正文跟随用户设置
  11. */
  12. // --- 读取微信系统字号系数(单次调用) ---
  13. export function readSystemFontScale() {
  14. try {
  15. if (typeof uni !== 'undefined' && uni.getAppBaseInfo) {
  16. const baseInfo = uni.getAppBaseInfo()
  17. if (baseInfo) {
  18. // 优先:直接缩放比例(微信基础库 >= 2.23.4 支持)
  19. if (typeof baseInfo.fontSizeScaleFactor === 'number' && !isNaN(baseInfo.fontSizeScaleFactor)) {
  20. return baseInfo.fontSizeScaleFactor
  21. }
  22. // 降级:原始 px 值除以基准 16px
  23. if (typeof baseInfo.fontSizeSetting === 'number' && !isNaN(baseInfo.fontSizeSetting)) {
  24. const scale = baseInfo.fontSizeSetting / 16
  25. if (scale > 0) return scale
  26. }
  27. }
  28. }
  29. } catch (e) {
  30. // ignore
  31. }
  32. try {
  33. if (typeof wx !== 'undefined' && wx.getAppBaseInfo) {
  34. const baseInfo = wx.getAppBaseInfo()
  35. if (baseInfo) {
  36. if (typeof baseInfo.fontSizeScaleFactor === 'number' && !isNaN(baseInfo.fontSizeScaleFactor)) {
  37. return baseInfo.fontSizeScaleFactor
  38. }
  39. if (typeof baseInfo.fontSizeSetting === 'number' && !isNaN(baseInfo.fontSizeSetting)) {
  40. const scale = baseInfo.fontSizeSetting / 16
  41. if (scale > 0) return scale
  42. }
  43. }
  44. }
  45. } catch (e) {
  46. // ignore
  47. }
  48. return 1
  49. }
  50. // --- 钳制系数到 [0.9, 1.7] 并返回保留两位小数的值 ---
  51. function clampAndRound(scale) {
  52. if (typeof scale !== 'number' || isNaN(scale) || scale <= 0) return 1
  53. if (scale < 0.9) scale = 0.9
  54. if (scale > 1.7) scale = 1.7
  55. return Math.round(scale * 100) / 100
  56. }
  57. // --- 读取用户手动设置的字号系数(storage 持久化) ---
  58. export function getUserFontScale() {
  59. try {
  60. const stored = uni.getStorageSync('userFontScale')
  61. if (typeof stored === 'number' && !isNaN(stored) && stored > 0) {
  62. return clampAndRound(stored)
  63. }
  64. } catch (e) {
  65. // ignore
  66. }
  67. return null // null 表示未设置
  68. }
  69. // --- 写入用户手动设置的字号系数 ---
  70. export function setUserFontScale(scale) {
  71. let s = clampAndRound(scale)
  72. try {
  73. uni.setStorageSync('userFontScale', s)
  74. } catch (e) {
  75. // ignore
  76. }
  77. return s
  78. }
  79. // --- 对外接口:读取当前生效字号系数 ---
  80. export function getFontScale() {
  81. // 优先用户设置
  82. const user = getUserFontScale()
  83. if (user !== null) return user
  84. // 其次 storage 中的系统系数(上次同步留下的缓存)
  85. try {
  86. const stored = uni.getStorageSync('fontSizeScaleFactor')
  87. if (typeof stored === 'number' && !isNaN(stored) && stored > 0) {
  88. return clampAndRound(stored)
  89. }
  90. } catch (e) {
  91. // ignore
  92. }
  93. // 最终:实时从系统读取
  94. return clampAndRound(readSystemFontScale())
  95. }
  96. // --- 同步(冷启动 / 每次回到前台)---
  97. export function syncFontScale() {
  98. const systemScale = readSystemFontScale()
  99. const scale = clampAndRound(systemScale)
  100. try {
  101. // 仅在用户未手动设置时写入 storage,避免把系统值覆盖掉用户的自定义偏好
  102. if (getUserFontScale() === null) {
  103. uni.setStorageSync('fontSizeScaleFactor', scale)
  104. }
  105. } catch (e) {
  106. // ignore
  107. }
  108. return scale
  109. }
  110. // --- 构建 mp-html 标签默认样式(用于 p/div/section/li/blockquote 等正文标签)
  111. // baseFontSize: 基准字号 rpx(推荐 28~32),例如文章/商品介绍正文用 32
  112. // scale: 当前生效字号系数(调用 getFontScale() 获取)
  113. // 返回 tagStyle 对象,可直接传给 <mp-html :tag-style="tagStyle">
  114. export function buildContentTagStyle(baseFontSize, scale) {
  115. const s = typeof scale === 'number' ? scale : 1
  116. const size = Math.round(baseFontSize * s)
  117. return {
  118. p: 'font-size:' + size + 'rpx;color:#444;line-height:1.8;',
  119. div: 'font-size:' + size + 'rpx;color:#444;line-height:1.8;',
  120. section: 'font-size:' + size + 'rpx;color:#444;line-height:1.8;',
  121. li: 'font-size:' + size + 'rpx;color:#444;line-height:1.8;',
  122. blockquote: 'font-size:' + size + 'rpx;color:#666;line-height:1.7;'
  123. }
  124. }