網頁字體預設
只需提供字體名稱,即可使用來自 Google Fonts、FontShare 的網頁字體。
查看所有支援的供應商。
安裝
bash
pnpm add -D @unocss/preset-web-fonts
bash
yarn add -D @unocss/preset-web-fonts
bash
npm install -D @unocss/preset-web-fonts
ts
import presetUno from '@unocss/preset-uno'
import presetWebFonts from '@unocss/preset-web-fonts'
import { defineConfig } from 'unocss'
export default defineConfig({
presets: [
presetUno(),
presetWebFonts({ /* options */ }),
],
})
提示
此預設包含在 unocss
套件中,您也可以從那裡導入它
ts
import { presetWebFonts } from 'unocss'
供應商
目前支援的供應商
none
- 不執行任何操作,將字體視為系統字體google
- Google Fontsbunny
- 注重隱私的 Google Fontsfontshare
- ITF 提供的優質字體服務
資訊
歡迎提交 PR 以新增更多供應商。🙌
自訂擷取函式
使用您自己的函式來擷取字體來源。
ts
import presetUno from '@unocss/preset-uno'
import presetWebFonts from '@unocss/preset-web-fonts'
import axios from 'axios'
import ProxyAgent from 'proxy-agent'
import { defineConfig } from 'unocss'
export default defineConfig({
presets: [
presetUno(),
presetWebFonts({
// use axios with an https proxy
customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://127.0.0.1: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,在瀏覽器中無法使用。