react native 处理iOS和安卓系统文字

栏目: IOS · Android · 发布时间: 6年前

内容简介:ios 端完成, 你可以试着修改系统的文字大小实验在 MainApplication.java 文件中加入如下代码:android 端也完成了
  1. 新增addCustomProps.js (位置随意放到项目目录, 只要路径引用的到) 下面是 addCustomProps.js 的内容
/**
 * 添加组件的的自定义属性
 * @param WrapComponent 组件
 * @param customProps 默认属性
 */
export default function addCustomProps(WrapComponent, customProps) {
	const componentRender = WrapComponent.prototype.render
	const componentDefaultProps = WrapComponent.prototype.constructor.defaultProps
	WrapComponent.prototype.constructor.defaultProps = {
		...componentDefaultProps,
		...customProps
	}
	WrapComponent.prototype.render = function render() {
		const oldProps = this.props

		this.props = {
			...this.props,
			style: [customProps.style, oldProps.style]
		}
		return componentRender.apply(this)
	}
}
复制代码
  1. 在你app的入口文件里加上如下内容 (:warning:注意 是入口文件 ,否则可能不起作用)
import React, { Component } from 'react'
import { Text, TextInput } from 'react-native'

// 处理iOS系统文字
addCustomProps(Text, {allowFontScaling: false});
addCustomProps(TextInput, {allowFontScaling: false});
复制代码

ios 端完成, 你可以试着修改系统的文字大小实验

android 处理方法如下 :

在 MainApplication.java 文件中加入如下代码:

import android.content.res.Configuration;
import android.content.res.Resources;

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    if (newConfig.fontScale != 1) // 非默认值
      getResources();
    super.onConfigurationChanged(newConfig);
  }

  @Override
  public Resources getResources() {
    Resources res = super.getResources();
    if (res.getConfiguration().fontScale != 1) { // 非默认值
      Configuration newConfig = new Configuration();
      newConfig.setToDefaults(); // 设置默认
      res.updateConfiguration(newConfig, res.getDisplayMetrics());
    }
    return res;
  }
复制代码

android 端也完成了


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Docker开发指南

Docker开发指南

[英] Adrian Mouat / 黄彦邦 / 人民邮电出版社 / 2017-4 / 79.00元

Docker容器轻量和可移植的特性尤其适用于动态和分布式的环境,它的兴起给软件开发流程带来了一场革命。本书对Docker进行了全面讲解,包括开发、生产以至维护的整个软件生命周期,并对其中可能出现的一些问题进行了探讨,如软件版本差异、开发环境与生产环境的差异、系统安全问题,等等。一起来看看 《Docker开发指南》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具