Home  >  Article  >  Web Front-end  >  What should I do if the font icon is not displayed after uniapp is packaged?

What should I do if the font icon is not displayed after uniapp is packaged?

PHPz
PHPzOriginal
2023-04-18 09:47:071556browse

In recent years, with the continuous development of front-end technology, font icons, as a lightweight icon solution, have been widely used in web and mobile design. When using uniapp to develop mobile applications, we can also easily use font icons, but there is often a problem that the font icons cannot be displayed properly after packaging. Today, we will discuss the reasons and solutions for why the font icon does not display after uniapp is packaged.

Cause analysis

1. The font file was not loaded successfully

First of all, we need to understand that uniapp will generate a dist directory after packaging, and the content in this directory will be our final Published applications include various resource files and HTML files. When we use font icons, we actually use CSS styles in the HTML file and set the data source to the font file of the font icon, so the problem may occur in the HTML file or font file.

The most common situation is that the font file is not loaded successfully, resulting in the font icon not being displayed. You can view the console output through the F12 developer tools. If there is a 404 or network error and the font file is in a failed request status, it can be determined that the font file was not loaded successfully.

2. Address error

Another possibility is that the address we introduced the font is wrong. Because uniapp uses relative paths to introduce resource files, you need to ensure that the HTML file and the font file are in the same file. Clip it down. If a file path error occurs, the font icon will not be displayed properly. Similarly, you can determine whether there is a path error through the console output.

Solution

1. Add local font resources

If the font file is not loaded successfully, we can try to add the font file to the local resource and then add it to the HTML file Use local relative paths for reference.

  1. Create a new fonts folder in the project root directory and extract the downloaded font files into the folder.
  2. Enter the uni.scss file and introduce the font file, as shown below:

    @font-face{
        font-family:'iconfont';
        src:url(../fonts/iconfont.ttf) format('truetype');
    }

    You need to pay attention here, @font-face The name of font-family needs to be consistent with the font name used in HTML.

  3. Use the font icon in the HTML file as follows:

    <i class="uni-icon uni-icon-wode"></i>

    Here the uni-icon-wode is what we have in the font file Customized icon class name, and uni-icon is the base class defined in uni.scss.

If this method solves the problem that the font icon cannot be displayed, then congratulations, the problem has been solved. However, sometimes even if we have followed the above methods, the font icon still cannot be displayed normally. In this case, you can try the following method.

2. Use cdn

Sometimes we find that the local reference of the font file fails, then we can consider using cdn to solve the problem. However, when using CDN, we need to ensure that the CDN address we introduce is correct, and since the CDN address may be affected by the network, it is best to provide multiple alternatives. The modification method is as follows:

  1. In the manifest.json file, add the resource address of the font file, as follows:

    "networkTimeout": {
       "request": 5000,
       "downloadFile": 10000
    },
       "onDemandResourceRules": [
       {
          "host": "xxxxx.com",
          "files": [
             "/fonts/iconfont.ttf"
          ]
       }
    ],
    "preloadRule": {
       "async": [
          {"path": "xxxxx.com/fonts/iconfont.ttf"}
       ],
       "sync": [
       ]
    }

    wherexxxxx.comSpecify the domain name of the cdn for us.

  2. Use the cdn address in the uni.scss file as follows:

    @font-face{
        font-family:'iconfont';
        src:url(//xxxxx.com/fonts/iconfont.ttf) format('truetype');
    }

    The // here means The relative path with the same protocol is used, suitable for http, https, etc.

  3. Use the font icon in the HTML file as follows:

    <i class="uni-icon uni-icon-wode"></i>

    Similarly, the uni-icon-wode here is what we have in the font The customized icon class name in the file.

It should be noted that using CDN may also encounter other problems, such as unstable connections, files exceeding cache limits, etc., so more testing and backup plans are required.

In general, font icons are a good and lightweight icon solution when using uniapp to develop mobile applications. However, due to issues such as the complexity of packaging and the way resources are handled, it may There may be situations where the font icon cannot be displayed properly. The above two methods can help us solve these problems and make our applications more beautiful and practical.

The above is the detailed content of What should I do if the font icon is not displayed after uniapp is packaged?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn