fontScale.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. export function readSystemFontScale() {
  2. try {
  3. if (typeof uni !== 'undefined' && uni.getAppBaseInfo) {
  4. const baseInfo = uni.getAppBaseInfo()
  5. if (baseInfo && typeof baseInfo.fontSizeScaleFactor === 'number' && !isNaN(baseInfo.fontSizeScaleFactor)) {
  6. return baseInfo.fontSizeScaleFactor
  7. }
  8. if (baseInfo && typeof baseInfo.fontSizeSetting === 'number' && !isNaN(baseInfo.fontSizeSetting)) {
  9. const scale = baseInfo.fontSizeSetting / 16
  10. if (scale > 0) {
  11. return scale
  12. }
  13. }
  14. }
  15. } catch (e) {
  16. // ignore
  17. }
  18. try {
  19. if (typeof wx !== 'undefined' && wx.getAppBaseInfo) {
  20. const baseInfo = wx.getAppBaseInfo()
  21. if (baseInfo && typeof baseInfo.fontSizeScaleFactor === 'number' && !isNaN(baseInfo.fontSizeScaleFactor)) {
  22. return baseInfo.fontSizeScaleFactor
  23. }
  24. if (baseInfo && typeof baseInfo.fontSizeSetting === 'number' && !isNaN(baseInfo.fontSizeSetting)) {
  25. const scale = baseInfo.fontSizeSetting / 16
  26. if (scale > 0) {
  27. return scale
  28. }
  29. }
  30. }
  31. } catch (e) {
  32. // ignore
  33. }
  34. return 1
  35. }
  36. export function getFontScale() {
  37. try {
  38. const stored = uni.getStorageSync('fontSizeScaleFactor')
  39. if (typeof stored === 'number' && !isNaN(stored) && stored > 0) {
  40. let scale = stored
  41. if (scale < 0.9) scale = 0.9
  42. if (scale > 1.7) scale = 1.7
  43. const rounded = Math.round(scale * 100) / 100
  44. return rounded
  45. }
  46. } catch (e) {
  47. // ignore
  48. }
  49. const systemScale = readSystemFontScale()
  50. let scale = systemScale
  51. if (isNaN(scale) || scale <= 0) {
  52. scale = 1
  53. }
  54. if (scale < 0.9) scale = 0.9
  55. if (scale > 1.7) scale = 1.7
  56. const rounded = Math.round(scale * 100) / 100
  57. return rounded
  58. }
  59. export function syncFontScale() {
  60. const systemScale = readSystemFontScale()
  61. let scale = systemScale
  62. if (isNaN(scale) || scale <= 0) {
  63. scale = 1
  64. }
  65. if (scale < 0.9) scale = 0.9
  66. if (scale > 1.7) scale = 1.7
  67. const rounded = Math.round(scale * 100) / 100
  68. try {
  69. uni.setStorageSync('fontSizeScaleFactor', rounded)
  70. } catch (e) {
  71. // ignore
  72. }
  73. return rounded
  74. }