async loadFontFaceFromCache1() {
let tempFilePath
const savedFilePath = uni.getStorageSync('PingFang')
try {
const res = await new Promise((resolve, reject) => {
uni.getSavedFileList({
success: (res) => resolve(res),
fail: (err) => reject(err)
})
})
const fileList = res.fileList
const file = fileList.find(file => file.filePath === savedFilePath)
if (file) {
tempFilePath = file.filePath
}
} catch (error) {
console.error('获取文件列表失败:', error)
}
if (!tempFilePath) {
try {
const res = await new Promise((resolve, reject) => {
uni.downloadFile({
url: 'https://www.dingjianyun.com/littleAppImages/djsoftTool/fonts/PingFangSC-Medium.otf',
success: (res) => resolve(res),
fail: (err) => reject(err)
})
})
tempFilePath = res.tempFilePath
uni.saveFile({
tempFilePath,
success(res) {
uni.setStorage({
key: 'PingFang',
data: res.savedFilePath
})
}
})
} catch (error) {
console.log('下载失败:', error)
return
}
} else {
console.log('使用缓存资源,跳过下载步骤')
}
// Vue 3 中的 loadFontFace 调用方式
try {
await new Promise((resolve, reject) => {
uni.loadFontFace({
family: 'PingFang SC',
source: `url("${plus.io.convertLocalFileSystemURL(tempFilePath)}")`,
success: (res) => {
console.log('字体加载成功:', res)
resolve(res)
},
fail: (err) => {
console.error('字体加载失败:', err)
reject(err)
},
complete: () => {
console.log('字体加载完成')
}
})
})
} catch (error) {
console.error('字体加载过程出错:', error)
}
},
Vue3 在iOS端App运行时 加载失败 vue2中可以正常加载
0 个回复