-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (72 loc) · 2.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Created by JetBrains WebStorm.
* Author: yoon
* Date: 18-7-30
* Time: 上午9:31
* Desc: 固定字体大小 不随系统字体大小而变化
* @use import in root file (index.js)
*/
import React from 'react'
import {
Text,
TextInput,
PixelRatio,
Platform,
} from 'react-native'
const ReactNativePropRegistry = require('ReactNativePropRegistry');
const fontSizeScale = PixelRatio.getFontScale()
const originTextRender = Text.prototype.render
const originTextInputRender = TextInput.prototype.render
function defaultFontSize() {
return Platform.OS === 'ios' ? 15 : 14
}
function fixedFontSize(target) {
return function () {
const originComponent = target.apply(this, arguments);
return React.cloneElement(originComponent, {
allowFontScaling: false
})
}
}
Text.prototype.render = fixedFontSize(originTextRender)
if (Platform.OS === 'ios') {
TextInput.prototype.render = fixedFontSize(originTextInputRender)
} else {
TextInput.prototype.render = function () {
const originComponent = originTextInputRender.apply(this, arguments);
try {
const originText = originComponent.props.children
let originStyle = originText.props.style
let newStyle = {
fontSize: defaultFontSize()
}
let textStyle = originStyle[0]
if (Number.isInteger(textStyle)) {
textStyle = ReactNativePropRegistry.getByID(textStyle)
}
if (Array.isArray(textStyle)) {
newStyle = textStyle.reduce((pre, curr) => {
let styleItem = curr || {}
if (Number.isInteger(styleItem)) {
styleItem = ReactNativePropRegistry.getByID(styleItem)
}
return {...pre, ...styleItem}
}, newStyle)
} else {
newStyle = {...newStyle, ...(textStyle || {})}
}
newStyle.fontSize = newStyle.fontSize / fontSizeScale
return React.cloneElement(originComponent, {
children: {
...originText,
props: {
...originText.props,
style: newStyle
}
}
})
} catch (e) {
return originComponent
}
}
}