-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (180 loc) · 5.66 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// main index.js
import {
ActivityIndicator,
Platform,
StyleSheet,
Text,
View,
findNodeHandle,
NativeModules
} from "react-native";
import React, { useEffect, useMemo, useRef, useState } from "react";
import RNFS from "react-native-fs";
const kFontLoadStatus = {
IDLE: "idle",
LOADING: "loading",
LOADEND: "loadend",
};
//todo: 用于缓存字体状态,避免一些不必要的文件存在判断、load到缓存等异步操作
const kFontStatusCache = {
// [fontName]: kFontLoadStatus.IDLE | kFontLoadStatus.LOADING | kFontLoadStatus.LOADEND
};
const ArtText = ({ text, style, fontInfo }) => {
let { fontUrl, fontName } = fontInfo;
console.log("fontUrl, fontName:: ", fontUrl, fontName);
const [fontLoadStatus, setFontLoadStatus] = useState(
fontUrl
? {
[fontUrl]: kFontLoadStatus.IDLE,
}
: null
);
const textCompRef = useRef();
const currentFontNameRef = useRef(null);
useEffect(() => {
console.log("useEffect~~", fontUrl, fontName);
//异步加载字体,载入缓存
if (fontUrl) {
setFontLoadStatus({
[fontUrl]: kFontLoadStatus.LOADING,
});
downloadIfNotExistWithUrl(fontUrl)
.then((path) => {
let fontPath = path;
console.log("path:::", path);
if (Platform.OS == "ios") {
// fontPath = path.split("/").pop();
} else {
fontPath.startsWith("/") && (fontPath = path.substring(1));
}
console.log("fontPath:::", fontPath);
// setTimeout(() => {
let fontNodeId = findNodeHandle(textCompRef.current);
if (!fontNodeId) {
console.log("fontNodeId is null");
setFontLoadStatus({
[fontUrl]: kFontLoadStatus.LOADEND,
});
return;
}
NativeModules.RemoteFontLoader.loadFontFile(fontPath, fontNodeId)
.then(() => {
console.log("loadFontFile success~");
currentFontNameRef.current = fontName;
})
.catch((err) => {
//todo: toast
console.log("loadFontFile err:::", err);
})
.finally(() => {
console.log("loadFontFile finally");
setFontLoadStatus({
[fontUrl]: kFontLoadStatus.LOADEND,
});
});
// }, 10);
})
.catch((err) => {
//todo: toast
console.log("downloadIfNotExist err:::", err);
setFontLoadStatus({
[fontUrl]: kFontLoadStatus.LOADEND,
});
});
}
return () => {};
}, [fontUrl, fontName]);
const textStyle = useMemo(() => {
// Fix: iOS 在字体未达到可应用状态时,提前为Text设置了fontfamily,debugging时会报错
if (fontLoadStatus?.[fontUrl] == kFontLoadStatus.LOADEND) {
return [
style,
{
//Fix: Android端通过原生去设置远程下载的字体,若再在style里设置fontFamily,会导致字体应用失效。。。
fontFamily: Platform.OS == "ios" ? fontName : undefined,
},
];
} else {
return style;
}
}, [fontLoadStatus, fontUrl, fontName, style]);
console.log("textStyle:", textStyle);
return (
<View style={{ backgroundColor: "red" }}>
<Text ref={textCompRef} style={textStyle}>
{text}
</Text>
{fontLoadStatus?.[fontUrl] == kFontLoadStatus.LOADING ? (
<ActivityIndicator
animating
size="small"
color="black"
style={{
justifyContent: "center",
alignItems: "center",
position: "absolute",
}}
/>
) : null}
</View>
);
};
export default ArtText;
const styles = StyleSheet.create({});
const fontsDir = RNFS.CachesDirectoryPath + "/fonts";
async function downloadIfNotExistWithUrl(url) {
console.log("downloadIfNotExistWithUrl:", url);
if (!url) {
return Promise.reject({ code: -1, msg: "url is null" });
}
let fileName = url.substr(url.lastIndexOf("/") + 1);
if (!(fileName?.length > 0)) {
return Promise.reject({ code: -1, msg: "fileName is null" });
}
// yousheziyoubangbangti3.ttf
console.log("fileName::", fileName);
let filePath = fontsDir + "/" + fileName;
// /var/mobile/Containers/Data/Application/5D44C3B2-A022-49D6-8FE2-5F06CA2E03EF/Library/Caches/fonts/yousheziyoubangbangti3.ttf
let isExist = await RNFS.exists(fontsDir);
console.log("RNFS.exists fontsDir:", isExist);
if (!isExist) {
let mkdirResult = await RNFS.mkdir(fontsDir);
console.log("mkdir fontsDir: ", mkdirResult);
}
let result = await downloadIfNotExistWithFilePath(filePath, url);
return result;
}
function downloadIfNotExistWithFilePath(filePath, url) {
console.log("downloadIfNotExistWithFilePath::", filePath);
return RNFS.exists(filePath)
.then((isExist) => {
if (isExist) {
console.log("isExist ~:", filePath);
return filePath;
} else {
return RNFS.downloadFile({
fromUrl: url,
toFile: filePath,
background: true,
discretionary: true,
})
.promise.then((res) => {
if (res && res.statusCode == 200) {
console.log("download ok ~");
return filePath;
} else {
console.log("文件下载错误:", res);
return Promise.reject(res);
}
})
.catch((err) => {
console.log("文件下载错误:::", err);
return Promise.reject(err);
});
}
})
.catch((err) => {
console.log("文件判断错误:::::", err);
return Promise.reject(err);
});
}