load-font-face.uvue 3.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
<template>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
  <page-head title="loadFontFace"></page-head>
  <view class="uni-padding-wrap">
    <text class="font-size-20">全局加载字体:</text>
    <text class="font-size-20 line-height-40" style="font-family: UniFontFamily">font-family: uni.ttf</text>
    <view style="flex-direction: row;">
      <text class="font-size-20" style="font-family: UniFontFamily;">{{
        uniIcon1
      }}</text>
      <text style="margin-left:5px;margin-right: 20px;line-height:22px;">\ue100</text>
      <text class="font-size-20" style="font-family: UniFontFamily;">{{
        uniIcon2
      }}</text>
      <text style="margin-left:5px;line-height:22px;">\ue101</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
15
    </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
16 17 18 19 20 21 22 23
    <text class="uni-common-mt font-size-20">非全局加载字体:</text>
    <text class="font-size-20 line-height-40" style="font-family: AlimamaDaoLiTiTTF">font-family: 阿里妈妈刀隶体-ttf
      (网络字体下载后生效)</text>
    <text class="font-size-20 line-height-40" style="font-family: AlimamaDaoLiTiOTF">font-family:
      阿里妈妈刀隶体-otf</text>
    <text class="item" style="font-family: AlimamaDaoLiTiWOFF">font-family: 阿里妈妈刀隶体-woff</text>
    <text class="item" style="font-family: AlimamaDaoLiTiWOFF2">font-family: 阿里妈妈刀隶体-woff2</text>
    <button class="uni-btn" @click="navigateToChild">跳转子页面测试字体生效范围</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
24
  </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
25 26
</template>
<script>
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28 29 30 31 32 33 34 35 36 37
  export default {
    data() {
      return {
        uniIcon1: '\ue100',
        uniIcon2: '\ue101',
      }
    },
    onLoad() {
      uni.loadFontFace({
        global: true,
        family: 'UniFontFamily',
DCloud-WZF's avatar
DCloud-WZF 已提交
38
        source: "url('/static/font/uni.ttf')",
DCloud-WZF's avatar
DCloud-WZF 已提交
39 40 41 42 43 44 45 46 47 48
        success() {
          console.log('global loadFontFace uni.ttf success')
        },
        fail(error) {
          console.warn('global loadFontFace uni.ttf fail', error.errMsg)
        },
      })
      uni.loadFontFace({
        family: 'AlimamaDaoLiTiTTF',
        source:
49
          "url('https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/font/AlimamaDaoLiTi.ttf')",
DCloud-WZF's avatar
DCloud-WZF 已提交
50 51 52 53 54 55 56 57 58
        success() {
          console.log('loadFontFace Remote AlimamaDaoLiTi.ttf success')
        },
        fail(error) {
          console.warn('loadFontFace Remote AlimamaDaoLiTi.ttf fail', error.errMsg)
        },
      })
      uni.loadFontFace({
        family: 'AlimamaDaoLiTiOTF',
DCloud-WZF's avatar
DCloud-WZF 已提交
59
        source: "url('/static/font/AlimamaDaoLiTi.otf')",
DCloud-WZF's avatar
DCloud-WZF 已提交
60 61 62 63 64 65 66 67 68
        success() {
          console.log('loadFontFace AlimamaDaoLiTi.otf success')
        },
        fail(error) {
          console.warn('loadFontFace AlimamaDaoLiTi.otf fail', error.errMsg)
        },
      })
      uni.loadFontFace({
        family: 'AlimamaDaoLiTiWOFF',
DCloud-WZF's avatar
DCloud-WZF 已提交
69
        source: "url('/static/font/AlimamaDaoLiTi.woff')",
DCloud-WZF's avatar
DCloud-WZF 已提交
70 71 72 73 74 75 76 77 78
        success() {
          console.log('loadFontFace AlimamaDaoLiTi.woff success')
        },
        fail(error) {
          console.warn('loadFontFace AlimamaDaoLiTi.woff fail', error.errMsg)
        },
      })
      uni.loadFontFace({
        family: 'AlimamaDaoLiTiWOFF2',
DCloud-WZF's avatar
DCloud-WZF 已提交
79
        source: "url('/static/font/AlimamaDaoLiTi.woff2')",
DCloud-WZF's avatar
DCloud-WZF 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        success() {
          console.log('loadFontFace AlimamaDaoLiTi.woff2 success')
        },
        fail(error) {
          console.warn('loadFontFace AlimamaDaoLiTi.woff2 fail', error.errMsg)
        },
      })
    },
    methods: {
      navigateToChild() {
        uni.navigateTo({
          url: '/pages/API/load-font-face/load-font-face-child',
        })
      },
    },
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
96 97 98
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
99 100 101
  .font-size-20 {
    font-size: 20px;
  }
W
wanganxp 已提交
102

DCloud-WZF's avatar
DCloud-WZF 已提交
103 104 105
  .line-height-40 {
    line-height: 40px;
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
106
</style>