Web 字体预设
通过简单地提供字体名称来使用来自 Google Fonts 和 FontShare 的 Web 字体。
查看 所有支持的提供者。
安装
bash
pnpm add -D @unocss/preset-web-fonts
bash
yarn add -D @unocss/preset-web-fonts
bash
npm install -D @unocss/preset-web-fonts
bash
bun add -D @unocss/preset-web-fonts
ts
import presetWebFonts from '@unocss/preset-web-fonts'
import presetWind3 from '@unocss/preset-wind3'
import { defineConfig } from 'unocss'
export default defineConfig({
presets: [
presetWind3(),
presetWebFonts({ /* options */ }),
],
})
TIP
此预设包含在 unocss
包中,您也可以从那里导入它:
ts
import { presetWebFonts } from 'unocss'
提供者
当前支持的提供者:
none
- 不执行任何操作,将字体视为系统字体google
- Google Fontsbunny
- 隐私友好的 Google Fontsfontshare
- ITF 提供的优质字体服务
INFO
欢迎提交 PR 以添加更多提供者。🙌
自定义获取函数
使用您自己的函数获取字体源。
ts
import presetWebFonts from '@unocss/preset-web-fonts'
import presetWind3 from '@unocss/preset-wind3'
import axios from 'axios'
import ProxyAgent from 'proxy-agent'
import { defineConfig } from 'unocss'
export default defineConfig({
presets: [
presetWind3(),
presetWebFonts({
// 使用带有 https 代理的 axios
customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }).then(it => it.data),
provider: 'google',
fonts: {
sans: 'Roboto',
mono: ['Fira Code', 'Fira Mono:400,700'],
},
}),
],
})
选项
provider
- 类型:
WebFontsProviders
- 默认:
google
Web 字体的提供者服务。
ts
type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none'
fonts
- 类型:
Record<string, WebFontMeta | string | (WebFontMeta | string)[]>
字体。有关更多详细信息,请参见 示例。
ts
interface WebFontMeta {
name: string
weights?: (string | number)[]
italic?: boolean
/**
* 覆盖提供者
* @default <匹配根配置>
*/
provider?: WebFontsProviders
}
extendTheme
- 类型:
boolean
- 默认:
true
扩展主题对象。
themeKey
- 类型:
string
- 默认:
fontFamily
主题对象的键。
inlineImports
- 类型:
boolean
- 默认:
true
内联 CSS @import()
。
customFetch
- 类型:
(url: string) => Promise<string>
- 默认:
undefined
使用您自己的函数获取字体源。有关更多信息,请参见 自定义获取函数。
示例
ts
presetWebFonts({
provider: 'google', // 默认提供者
fonts: {
// 这些将扩展默认主题
sans: 'Roboto',
mono: ['Fira Code', 'Fira Mono:400,700'],
// 自定义字体
lobster: 'Lobster',
lato: [
{
name: 'Lato',
weights: ['400', '700'],
italic: true,
},
{
name: 'sans-serif',
provider: 'none',
},
],
},
})
将自动生成以下 CSS:
css
@import url('https://fonts.googleapis.com/css2?family=Roboto&family=Fira+Code&family=Fira+Mono:wght@400;700&family=Lobster&family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap');
/* layer: default */
.font-lato {
font-family: "Lato", sans-serif;
}
.font-lobster {
font-family: "Lobster";
}
.font-mono {
font-family: "Fira Code", "Fira Mono", ui-monospace, SFMono-Regular, Menlo,
Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
.font-sans {
font-family: "Roboto", ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans",
sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji";
}
本地服务字体
默认情况下,预设将从提供者的 CDN 中获取字体。如果您想要在本地提供字体,可以下载字体并使用 @unocss/preset-web-fonts/local
中的处理器从您自己的服务器提供它们。
ts
import presetWebFonts from '@unocss/preset-web-fonts'
import { createLocalFontProcessor } from '@unocss/preset-web-fonts/local'
import { defineConfig } from 'unocss'
export default defineConfig({
presets: [
presetWebFonts({
provider: 'none',
fonts: {
sans: 'Roboto',
mono: 'Fira Code',
},
// 这将下载字体并在本地提供
processors: createLocalFontProcessor({
// 缓存字体的目录
cacheDir: 'node_modules/.cache/unocss/fonts',
// 保存字体资产的目录
fontAssetsDir: 'public/assets/fonts',
// 客户端提供字体的基本 URL
fontServeBaseUrl: '/assets/fonts'
})
}),
],
})
这将把字体资产下载到 public/assets/fonts
并从客户端的 /assets/fonts
提供它们。在执行此操作时,请确保字体的许可证允许您如此重新分发,工具不对任何法律问题负责。
INFO
此功能特定于 Node.js,无法在浏览器中使用。