【uniapp】uniapp设置改变全局字体大小功能:

文章目录


一、效果:




二、文档:

**思路:**通过保存需要的字体大小,然后在页面设置page-meta设置根节点大小,来实现控制字体的大小
custom-tab-bar | uni-app官网
uniapp改变设置改变全局字体大小功能_uniapp根据设计稿设置根元素的字体大小-CSDN博客
uniapp设置字体大小,老年模式

三、案例:引用插件,将项目中的字体单位rpx换成rem,结合官网提供的page-meta属性,通过控制根字体大小,达到调节字体大小要求。

1.安装第三方插件postcss-px-to-viewport

vue 复制代码
npm install postcss-px-to-viewport --save-dev

2.项目根目录创建postcss.config.js,并写入配置:

javascript 复制代码
// postcss.config.js

const path = require('path')
module.exports = {
  parser: 'postcss-comment',
  plugins: {
    'postcss-import': {
      resolve(id, basedir, importOptions) {
        if (id.startsWith('~@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3))
        } else if (id.startsWith('@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2))
        } else if (id.startsWith('/') && !id.startsWith('//')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1))
        }
        return id
      }
    },
    'autoprefixer': {
      overrideBrowserslist: ["Android >= 4", "ios >= 8"],
      remove: process.env.UNI_PLATFORM !== 'h5'
    },
    // 借助postcss-px-to-viewport插件,实现rpx转px,文档:https://github.com/evrone/postcss-px-to-viewport/blob/master/README_CN.md
    // 以下配置,可以将rpx转换为1/2的px,如20rpx=10px,如果要调整比例,可以调整 viewportWidth 来实现
    'postcss-px-to-viewport': {
      unitToConvert: 'rpx', // 需要转换的单位
      viewportWidth: 750,
      unitPrecision: 5,
      propList: ['*'],
      fontViewportUnit: 'rem', // 字体需要转成的单位,只针对 font-size 属性
      selectorBlackList: [],
      minPixelValue: 1,
      mediaQuery: false,
      replace: true,
      exclude: undefined,
      include: undefined,
      landscape: false
    },
    '@dcloudio/vue-cli-plugin-uni/packages/postcss': {}
  }
}

3.根据页面设置,vuex取出设置大小

vue 复制代码
<template>
  <view>
    <view class="uni-padding-wrap uni-common-mt" style="margin-top: 50rpx;">
      <view class="titleBox">
        <view style="font-size:25rpx">小</view>
        <view style="font-size:30rpx">标准</view>
        <view style="font-size:35rpx">中</view>
        <view style="font-size:40rpx">大</view>
      </view>
      <view>
        <slider min="2.6" max="5.6" :value="fontValue" @change="sliderChange" show-value step="1"/>
      </view>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        fontValue:3.6,
      }
    },
    onLoad() {
      this.fontValue = this.$store.state.scaleFontSize
    },
    methods: {
      sliderChange(e) {
        // console.log('value 发生变化:' + e.detail.value)
        this.$store.commit('setScaleFontSize',e.detail.value)
        console.log(this.$store.state.scaleFontSize)
        // 使用uni.reLaunch跳转到某个页面,并关闭所有页面
        uni.reLaunch({
          url:"../login/login"
        })
      },
    }
  }
</script>
<style>
  .titleBox{
    width: 80%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 30upx 0;
    margin-left: 5%;
  }

</style>

4.在每个页面中通过添加page-meta调节,page-meta 配置项参考链接

vue 复制代码
<template>
  <page-meta :root-font-size="`${this.$store.state.scaleFontSize}px`"></page-meta>
  <view></view>
</template>

此种方法navbar和tabbar的字体大小不能改变。

相关推荐
大叔_爱编程13 小时前
基于用户评论的热点问题挖掘与反馈分析系统-django+spider+uniapp
python·django·uni-app·毕业设计·源码·课程设计·spider
源码潇潇和逸逸1 天前
独立部署高校圈子平台:PHP+UniApp打造社交+交易+服务一站式校园解决方案
开发语言·uni-app·php
2501_916008891 天前
2026 iOS 证书管理,告别钥匙串依赖,构建可复制的签名环境
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS App 拿不到数据怎么办?数据解密导出到分析结构方法
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_916008892 天前
iOS App 抓包看不到内容,从有请求没数据一步步排查
android·ios·小程序·https·uni-app·iphone·webview
扶苏10022 天前
记一次 uni-app开发微信小程序 textarea 的“伪遮挡”踩坑实录
微信小程序·小程序·uni-app
RuoyiOffice2 天前
企业请假销假系统设计实战:一张表、一套流程、两段生命周期——BPM节点驱动的表单变形术
java·spring·uni-app·vue·产品运营·ruoyi·anti-design-vue
KongHen022 天前
uniapp-x实现自定义tabbar
前端·javascript·uni-app·unix
RuoyiOffice2 天前
SpringBoot+Vue3+Uniapp实现PC+APP双端考勤打卡设计:GPS围栏/内网双模打卡、节假日方案、定时预生成——附数据结构和核心源码讲解
java·spring·小程序·uni-app·vue·产品运营·ruoyi
2501_915921433 天前
2026 iOS 上架新趋势 iOS 发布流程模块化
android·ios·小程序·https·uni-app·iphone·webview