当前位置: 首页 > news >正文

uni-app 项目中使用自定义字体

文章目录

  • 前言
    • 🚨 uni-app 自定义字体兼容性分析
      • 📱 各平台兼容性对比
        • 1. **H5 平台** ✅ 兼容性最好
        • 2. **微信小程序** ⚠️ 有限制
        • 3. **APP 平台** ⚠️ 需特殊处理
      • 🔧 兼容性解决方案
      • 💡 推荐的兼容性方案
        • 方案1:使用系统字体(推荐)⭐
        • 方案2:条件编译处理(如果必须使用自定义字体)
      • 🛠️ 实际应用到组件
      • 📋 各平台具体问题
        • 1. **微信小程序限制**
        • 2. **APP 平台问题**
        • 3. **H5 平台**
      • 🎯 最佳实践建议
      • 💻 推荐的实现代码


前言

在 uni-app 项目中使用自定义字体确实存在一些兼容性问题。分析一下各个平台的兼容性情况:

🚨 uni-app 自定义字体兼容性分析

根据 uni-app 的跨平台特性,详细说明各平台的情况:

📱 各平台兼容性对比

1. H5 平台 ✅ 兼容性最好
// H5 平台完全支持 @font-face
@font-face {font-family: 'PingFang SC';src: url('/static/fonts/PingFang-Regular.ttf') format('truetype');font-weight: 400;
}
2. 微信小程序 ⚠️ 有限制
// 微信小程序不支持 @font-face,但支持系统字体
.text {/* 只能使用系统预装字体 */font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
3. APP 平台 ⚠️ 需特殊处理
  • Android: 需要将字体文件放到 static 目录,通过特定方式引用
  • iOS: 需要在 manifest.json 中配置字体,并注册字体

🔧 兼容性解决方案

使用条件编译来处理不同平台的兼容性:

兼容性最好的解决方案:

💡 推荐的兼容性方案

方案1:使用系统字体(推荐)⭐
// 在 src/style/index.scss 中定义
:root, page {// 使用系统字体栈,兼容性最佳font-family: /* #ifdef H5 */'PingFang SC', /* #endif *//* #ifdef MP-WEIXIN */'PingFang SC', /* #endif *//* #ifdef APP-PLUS */'PingFang SC', /* #endif */-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
}// 字体权重工具类
.font-regular { font-weight: 400; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }
方案2:条件编译处理(如果必须使用自定义字体)
// 只在 H5 平台使用自定义字体
/* #ifdef H5 */
@font-face {font-family: 'PingFang SC Custom';src: url('/static/fonts/PingFang-Regular.ttf') format('truetype');font-weight: 400;font-display: swap;
}@font-face {font-family: 'PingFang SC Custom';src: url('/static/fonts/PingFang-Medium.ttf') format('truetype');font-weight: 500;font-display: swap;
}@font-face {font-family: 'PingFang SC Custom';src: url('/static/fonts/PingFang-Bold.ttf') format('truetype');font-weight: 600;font-display: swap;
}
/* #endif */// 通用字体设置
.custom-font {font-family: /* #ifdef H5 */'PingFang SC Custom',/* #endif */'PingFang SC',-apple-system,BlinkMacSystemFont,sans-serif;
}

🛠️ 实际应用到组件

具有更好的兼容性:

.main-stats {margin-bottom: 16rpx;.amount {// 兼容性最好的字体设置font-family: 'PingFang SC',-apple-system,BlinkMacSystemFont,'Helvetica Neue',Helvetica,Arial,sans-serif;font-weight: 600;font-size: 40rpx;color: #ff6a00;line-height: 50rpx;margin-bottom: 4rpx;// 优化字体渲染/* #ifdef H5 */-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-rendering: optimizeLegibility;/* #endif */}.label {font-family: 'PingFang SC',-apple-system,BlinkMacSystemFont,sans-serif;font-size: 26rpx;color: #333;font-weight: 500;line-height: 1.2;}
}

📋 各平台具体问题

1. 微信小程序限制
  • ❌ 不支持 @font-face
  • ❌ 不能加载外部字体文件
  • ✅ 支持系统预装字体
  • ✅ 支持字体权重设置
2. APP 平台问题
  • ⚠️ Android 需要特殊配置
  • ⚠️ iOS 需要在 Info.plist 中注册
  • ⚠️ 字体文件路径处理复杂
  • ⚠️ 包体积会增大
3. H5 平台
  • ✅ 完全支持 @font-face
  • ✅ 支持多种字体格式
  • ⚠️ 需要考虑加载性能
  • ⚠️ 可能有跨域问题

🎯 最佳实践建议

  1. 优先使用系统字体PingFang SC 在 iOS 和 macOS 上是系统预装字体
  2. 设置字体回退:确保在不支持的平台上有合适的替代字体
  3. 使用条件编译:针对不同平台做特殊处理
  4. 测试各平台:在所有目标平台上进行充分测试

💻 推荐的实现代码

// src/style/index.scss
:root, page {// 兼容性最佳的字体设置font-family: 'PingFang SC',           // iOS/macOS 系统字体'Microsoft YaHei',       // Windows 中文字体'Hiragino Sans GB',      // macOS 中文字体-apple-system,           // iOS 系统字体BlinkMacSystemFont,      // macOS 系统字体'Segoe UI',             // Windows 系统字体Roboto,                 // Android 系统字体'Helvetica Neue',Arial,sans-serif;// 字体渲染优化/* #ifdef H5 */-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-rendering: optimizeLegibility;/* #endif */
}// 字体权重工具类
.font-light { font-weight: 300; }
.font-regular { font-weight: 400; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }

这样设置既能保证在支持 PingFang SC 的平台上显示期望的字体效果,又能在不支持的平台上有合适的降级处理,兼容性最佳!


文章转载自:

http://XIqTsGXq.nwczt.cn
http://Q3Yu4Sq0.nwczt.cn
http://209Oo7CV.nwczt.cn
http://mNTGP8OE.nwczt.cn
http://SEmJJLbS.nwczt.cn
http://O4APQl83.nwczt.cn
http://56Qpbqva.nwczt.cn
http://t1aWu7ar.nwczt.cn
http://8x0s0bvh.nwczt.cn
http://ecFD7rV8.nwczt.cn
http://nwORmrdR.nwczt.cn
http://3Yu6hUfQ.nwczt.cn
http://iUljhhgx.nwczt.cn
http://OOzuwFlT.nwczt.cn
http://dnlJjvwV.nwczt.cn
http://V1CoMpyd.nwczt.cn
http://Z3lzjVvc.nwczt.cn
http://LhZmJt1y.nwczt.cn
http://nJTdz7sA.nwczt.cn
http://UaYMVqnq.nwczt.cn
http://2vL8idCN.nwczt.cn
http://yk7EzZv7.nwczt.cn
http://m9zBwVS1.nwczt.cn
http://wJmUf3Yc.nwczt.cn
http://U17hT9Ec.nwczt.cn
http://dgPyquJs.nwczt.cn
http://51lSGmkw.nwczt.cn
http://bAbhua0j.nwczt.cn
http://SStKNeV1.nwczt.cn
http://61H15Qne.nwczt.cn
http://www.dtcms.com/a/374403.html

相关文章:

  • springboot maven 多环境配置入门与实战
  • 时序数据库选型指南:基于大数据视角的IoTDB应用优势分析详解!
  • 炫光活体检测技术:通过光学技术实现高效、安全的身份验证,有效防御多种伪造手段。
  • sqlite3的加解密全过程
  • Django REST Framework 中 @action 装饰器详解
  • 【Docker】一键将运行中的容器打包成镜像并导出
  • LLVM 数据结构简介
  • MCP与http、websocket的关系
  • 【modbus学习】
  • 【linux】sed/awk命令检索区间日志
  • 瑞派虹泰环城总院 | 打造“一站式宠物诊疗空间”,定义全国宠物医疗新高度
  • 数据分析画图显示中文
  • 嵌入式ARM架构学习3——启动代码
  • 2025云计算趋势:Serverless与AI大模型如何赋能中小企业
  • 如何利用 AWS 服务器优化跨境电商和 SEO 战略?
  • 大数据毕业设计-基于Python的中文起点网小说数据分析平台(高分计算机毕业设计选题·定制开发·真正大数据)
  • 小程序开发单行日历可滑动
  • 项目日记 -日志系统 -搭建基础框架
  • 计算机网络第四章(4)——网络层《ARP协议》
  • 探迹SalesGPT
  • 带有 Attention 机制的 Encoder-Decoder 架构模型分析
  • 利用易语言编写,逻辑为按照数字越大抽取率越前
  • leetcode 219 存在重复元素II
  • Redis(缓存)
  • ARP 协议
  • 169.在Vue3中使用OpenLayers + D3实现地图区块呈现不同颜色的效果
  • 【C++】递归与迭代:两种编程范式的对比与实践
  • 【Java】设计模式——单例、工厂、代理模式
  • C++ ——一文读懂:Valgrind 检测内存泄漏
  • 代码随想录算法训练营第三十一天 | 合并区间、单调递增的数字