


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.
- Create a new
fonts
folder in the project root directory and extract the downloaded font files into the folder. -
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 offont-family
needs to be consistent with the font name used in HTML. -
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, anduni-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:
-
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": [ ] }
where
xxxxx.com
Specify the domain name of the cdn for us. -
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. -
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!

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

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

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

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

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

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

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
