search
HomeWeb Front-enduni-appWhat should I do if the font icon is not displayed after uniapp is packaged?

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></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></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
How do I handle local storage in uni-app?How do I handle local storage in uni-app?Mar 11, 2025 pm 07:12 PM

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

How to rename UniApp download filesHow to rename UniApp download filesMar 04, 2025 pm 03:43 PM

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

How to handle file encoding with UniApp downloadHow to handle file encoding with UniApp downloadMar 04, 2025 pm 03:32 PM

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

How do I use uni-app's geolocation APIs?How do I use uni-app's geolocation APIs?Mar 11, 2025 pm 07:14 PM

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

How do I manage state in uni-app using Vuex or Pinia?How do I manage state in uni-app using Vuex or Pinia?Mar 11, 2025 pm 07:08 PM

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

How do I make API requests and handle data in uni-app?How do I make API requests and handle data in uni-app?Mar 11, 2025 pm 07:09 PM

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

How do I use uni-app's social sharing APIs?How do I use uni-app's social sharing APIs?Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use uni-app's easycom feature for automatic component registration?How do I use uni-app's easycom feature for automatic component registration?Mar 11, 2025 pm 07:11 PM

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft