0.1.3 • Published 4 years ago

react-native-normalization-text v0.1.3

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

react-native-normalization-text

react-native 字体大小、样式规范化管理,自适应缩放组件。

功能介绍

字体缩放

根据不同设备屏幕尺寸调整字体大小,可自定义缩放规则。

未适配:

图片

适配:

图片

字体管理

一次配置(或者使用默认配置),不在app中到处书写字体样式,也促进UI设计输出规范的字体。

安装

npm install react-native-normaliztion-text --save

或者使用yarn

yarn add react-native-normaliztion-text 

使用

import {Text, H1, H2} from 'react-native-normalization-text';  
...

// jsx
<Text>不带自定义样式的Text<Text>
<H1>带h1预设样式、props的Text<H1>

// 接受RN中Text的属性和配置中映射的size、color属性
<H2 size="large" color="danger" style={{...}}>带h2预设样式、props的Text<H2>

Text除了rn提供的属性,新添以下属性:

propstype描述
scalableItemsArray<string>参与缩放的属性。例:'fontSize', 'paddingLeft'
sizeNumber字体大小。默认配置或者自定义配置中sizes的key
colorNumber | String字体颜色。默认配置或者自定义配置中colors的key
darknessNumber | String颜色亮加深/减弱。支持数字或者百分比。例:加深100亮度值:darkness={100} 减少10%亮度值: darkness="-10%"

更多配置

默认配置:

配置项type描述默认值
scaleFunction字体缩放方法见defaultConfig.scale
scalableItemsArray<string>参与缩放的属性['fontSize', 'lightHeight']
sizesObject罗列所有字号,sizes[key]的key映射为Text的fontSize样式。例:配置了sizes: {big: 36},则可使用<Text size="big" />见defaultConfig.sizes
colorsObject罗列所有字体颜色,类似sizes,colors[key]的key映射为Text的color样式。例:配置了colors: {danger: 'red'},则可使用 <Text color="danger" />见defaultConfig.colors
categoriesObject分级字体。比如在categories中预设H1,则可以使用<Text.H1 />见defaultConfig.categories

默认sizes规范:

namevalue
huge36
xxxLarger24
xxLarger20
xLarger18
lager16
normal14
small12
tiny10

默认colors规范:

namevalue
titlergba(0,0,0,0.85)
primaryrgba(0,0,0,0.65)
secondaryrgba(0,0,0,0.45)
disabledrgba(0,0,0,0.25)
borderrgba(0,0,0,0.15)
dividersrgba(0,0,0,0.09)
whitergb(255,255,255)
grey1rgba(255,255,255,0.85)
grey2rgba(255,255,255,0.65)
grey3rgba(255,255,255,0.45)
grey4rgba(255,255,255,0.25)
grey5rgba(255,255,255,0.15)
grey6rgba(255,255,255,0.09)
success#52c41a
warning#faad14
error#ff190c

默认可用字体:

  • H1
  • H2
  • H3
  • H4
  • PrimaryText
  • SmallText
  • TinyText

自定义配置:

  1. 在项目根目录执行初始化脚本,或者在根目录手动新建新建 text.config.js
  2. 根据自身需求配置需要调整的配置项,可以只修改你需要修改的配置项,其余的默认配置项仍会起作用。
  3. 在项目根目录下执行 更新脚本让自定义配置生效,或者使用重置脚本恢复默认配置。

脚本

script描述
node node_modules/react-native-norma/scripts/makeConfig.js --init初始化配置文件
node node_modules/react-native-norma/scripts/makeConfig.js --update读取自定义脚本
node node_modules/react-native-norma/scripts/makeConfig.js --reset重置成默认脚本

可以将脚本添加至package.json的script字段中,方便调用。

样式优先级:

jsx中style样式 > jsx中color、size属性映射样式 > config.categories.h1.style

属性配置优先级(scalableItems):

jsx中scalableItems > config.categories.h1.props.scalableItems > config.scalableItems

配置文件引用依赖:

配置文件支持引用项目中的依赖,支持 require('..') 和 import res from '..'两种形式,比如在需要引用项目中的样式:

// text.config.js
import colors from '../../Theme/colors';
import scale from '../../Theme/fontScale';

module.exports = {
    scale
    colors: {
        primary: colors.primary,
        success: colors.success,
        error: colors.error
    }
};

颜色支持:

React Native支持的颜色格式