Skip to content

网页字体预设

只需提供字体名称,即可使用来自 Google FontsFontShare 的网页字体。

查看 所有支持的供应商

源代码

安装

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
uno.config.ts
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 */ }),
  ],
})

提示

此预设包含在 unocss 包中,你也可以从该包中导入它:

ts
import { presetWebFonts } from 'unocss'

供应商

当前支持的供应商:

信息

欢迎提交 PR 以添加更多供应商。🙌

自定义获取函数

使用你自己的函数来获取字体源。

uno.config.ts
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({
      // use axios with an https proxy
      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

网页字体的提供商服务。

ts
type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none'

fonts

  • 类型: Record<string, WebFontMeta | string | (WebFontMeta | string)[]>

字体设置。有关更多详细信息,请参阅 示例

ts
interface WebFontMeta {
  name: string
  weights?: (string | number)[]
  italic?: boolean
  /**
   * Override the provider
   * @default <matches root config>
   */
  provider?: WebFontsProviders
}

extendTheme

  • 类型: boolean
  • 默认值: true

扩展主题对象。

themeKey

  • 类型: string
  • 默认值: fontFamily

主题对象的键。

inlineImports

  • 类型: boolean
  • 默认值: true

内联 CSS @import()

customFetch

  • 类型: (url: string) => Promise<string>
  • 默认值: undefined

使用你自己的函数来获取字体源。请参阅 自定义获取函数

示例

ts
presetWebFonts({
  provider: 'google', // default provider
  fonts: {
    // these will extend the default theme
    sans: 'Roboto',
    mono: ['Fira Code', 'Fira Mono:400,700'],
    // custom ones
    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',
      },
      // This will download the fonts and serve them locally
      processors: createLocalFontProcessor({
        // Directory to cache the fonts
        cacheDir: 'node_modules/.cache/unocss/fonts',

        // Directory to save the fonts assets
        fontAssetsDir: 'public/assets/fonts',

        // Base URL to serve the fonts from the client
        fontServeBaseUrl: '/assets/fonts'
      })
    }),
  ],
})

这将把字体资源下载到 public/assets/fonts 目录,并在客户端从 /assets/fonts 提供这些资源。执行此操作时,请确保字体的许可允许你进行重新分发,该工具不对任何法律问题负责。

信息

此功能特定于 Node.js,在浏览器中无法使用。