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

做一个能够控制app全部页面字体大小的功能

类似于微信设置中的,调节字体大小功能

PlanA:(在百度上查到的)

通过在页面设置page-meta,设置根节点大小,来控制页面元素rem单位的大小

https://uniapp.dcloud.io/component/page-meta?id=page-meta

具体步骤:

1.创建一个公共的base.js文件

export default {
    created() {
        const self = this;

    },
    mounted() {
        const self = this;
    },
    methods: {
        //设置字体
        getRootFontSize(){
            const self = this;
            var fontSize = getApp().globalData.rootFontSize;
            if(fontSize){
                return fontSize;
            }else{
                fontSize = uni.getStorageSync('root_font_size');
                if(fontSize){
                    getApp().globalData.rootFontSize=fontSize;
                }else{
                    fontSize='12px';//默认字体大小
                    self.setRootFontSize(fontSize);
                }
                return fontSize;
            }
        },
        setRootFontSize(fontSize){
            uni.setStorageSync('root_font_size',fontSize);
            getApp().globalData.rootFontSize=fontSize;
        },

    }
}

2.设置修改字体的页面

①在页面头加入page-meta

<page-meta :root-font-size="getRootFontSize()"></page-meta>

②引入base.js

import base from '@/common/base.js';
	export default {
		extends: base,

③我这里引入了一个类似于微信改变字体大小的滑动条,滑动条改变时触发函数,保存字体大小到内存中,注意改变根元素字体大小后,需要刷新页面才能展示出来,我使用的是uni.relaunch跳转到页面(可以跳转到对应页面,并关闭其余页面)

<template>
	<page-meta :root-font-size="getRootFontSize()"></page-meta>
	<view>
		<view class="uni-padding-wrap uni-common-mt" style="margin-top: 50upx;">
			<view class="titleBox">
				<view style="font-size:25upx">A</view>
				<view style="font-size:30upx">标准</view>
				<view style="font-size:35upx">A</view>
				<view style="font-size:40upx">A</view>
			</view>
			
			<view>
				<slider min="10" max="16" :value="fontValue" @change="sliderChange" show-value step="2"/>
			</view>
		</view>
	</view>
</template>
<script>
	import base from '@/common/base.js';
	export default {
		extends: base,
		data() {
			return {
				fontValue:12,
				rootFontSize:'12upx',
				fontStart:uni.getStorageSync('root_font_size') + '',
				fontEnd:uni.getStorageSync('root_font_size') + ''
			}
		},
		onLoad() {
			const self = this;
			//当前字体大小
			self.fontValue = parseInt(self.getRootFontSize().split('px')[0]);
			console.log(self.fontValue )
		},
		methods: {
			sliderChange(e) {
				console.log('value 发生变化:' + e.detail.value)
				const self = this;
				let nowFontSize = e.detail.value + 'px';
				console.log(nowFontSize)
				self.fontEnd = nowFontSize;
				console.log(nowFontSize)
				self.setRootFontSize(nowFontSize);
			},
            //这里设置一个事件,使用uni.reLaunch跳转到某个页面,并关闭所有页面
			/*onNavigationBarButtonTap() {
				uni.reLaunch({
					url:"../tabbar-4"
				})
			}*/
		}
	}
</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 :root-font-size="getRootFontSize()"></page-meta>

②引入base.js

③将元素的单位设置为rem

 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值