12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- function downloadFont(fontUrl, filePath, fontFamily) {
- uni.downloadFile({
- url: fontUrl,
- success: res => {
- uni.getFileSystemManager().saveFile({
- tempFilePath: res.tempFilePath,
- filePath,
- success: res => {
-
- loadFontFace(fontFamily, res.savedFilePath)
- }
- })
- }
- })
- }
- function loadFontFace(fontFamily, filePath) {
-
- uni.getFileSystemManager().readFile({
- filePath,
- encoding: 'base64',
- success: res => {
- uni.loadFontFace({
- global: true,
- scopes: ['webview', 'native'],
- family: fontFamily,
- source: `url("data:font/ttf;charset=utf-8;base64,${res.data}")`,
- success(res) {
- console.log(fontFamily + '加载成功:' + res.status)
- },
- fail: function(res) {
-
-
- },
- })
- }
- })
- }
- export const loadCloudFontFace = function(fontUrl, filename, fontFamily) {
- const filePath = `${wx.env.USER_DATA_PATH}/${filename}`
- uni.getFileSystemManager().access({
- path: filePath,
- success: () => {
- loadFontFace(fontFamily, filePath)
- console.log('从本地加载了字体');
- },
- fail: () => {
- downloadFont(fontUrl, filePath, fontFamily)
- console.log('从外部加载了字体', fontUrl);
- }
- })
- }
- export default loadCloudFontFace
|