load-font-face.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '动态加载字体',
  5. path: 'packageAPI/pages/media/load-font-face/load-font-face'
  6. }
  7. },
  8. data: {
  9. theme: 'light',
  10. fontFamily: 'Bitstream Vera Serif Bold',
  11. loaded: false,
  12. },
  13. onLoad() {
  14. this.setData({
  15. theme: wx.getSystemInfoSync().theme || 'light'
  16. })
  17. if (wx.onThemeChange) {
  18. wx.onThemeChange(({theme}) => {
  19. this.setData({theme})
  20. })
  21. }
  22. this.setData({
  23. loaded: false
  24. })
  25. },
  26. loadFontFace() {
  27. const self = this
  28. wx.loadFontFace({
  29. family: this.data.fontFamily,
  30. source: 'url("https://sungd.github.io/Pacifico.ttf")',
  31. success(res) {
  32. console.log(res.status)
  33. self.setData({loaded: true})
  34. },
  35. fail(res) {
  36. console.log(res.status)
  37. },
  38. complete(res) {
  39. console.log(res.status)
  40. }
  41. })
  42. },
  43. clear() {
  44. this.setData({loaded: false})
  45. },
  46. onLoad() {
  47. this.setData({
  48. theme: wx.getSystemInfoSync().theme || 'light'
  49. })
  50. if (wx.onThemeChange) {
  51. wx.onThemeChange(({theme}) => {
  52. this.setData({theme})
  53. })
  54. }
  55. }
  56. })