有没有一种方法可以在网站上添加自定义字体,而不使用图片、Flash或其他图形技术?
例如,我正在制作一个婚礼网站,发现有很多适合这个主题的漂亮字体。但是我找不到正确的方法将该字体添加到服务器上。那么如何通过CSS将该字体包含在HTML中呢?是否可以在不使用图形技术的情况下完成这项操作?
PrivateFontCollection pfont = new PrivateFontCollection();
pfont.AddFontFile(filename);
FontFamily ff = pfont.Families[0];
然后使用该字体在 Graphics
上进行绘制。
<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<style>
body {
font-family: 'Montserrat';font-size: 22px;
}
</style>
</head>
<body>
<h1>Montserrat</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</body>
</html>
还可以使用WOFF字体 - 例如这里
@font-face {
font-family: 'Plakat Fraktur';
src: url('/resources/fonts/plakat-fraktur-black-modified.woff') format('woff');
font-weight: bold;
font-style: normal;
}
Typeface.js的JavaScript方式:
使用typeface.js,您可以在网页中嵌入自定义字体,因此无需将文本渲染为图像。
与创建图像或使用Flash以显示所需字体的站点图形文本相反,您可以使用typeface.js并编写纯HTML和CSS,就像访问者已经在本地安装了字体一样。
@font-face {
font-family: "CustomFont";
src: url("CustomFont.eot");
src: url("CustomFont.woff") format("woff"),
url("CustomFont.otf") format("opentype"),
url("CustomFont.svg#filename") format("svg");
}
简单的解决方案是在 CSS 中使用 @font-face。
@font-face {
font-family: myFirstFont;
src: url(fileLocation);}
div{
font-family: myfirstfont;}
@font-face
得到了广泛支持并建议进行一般使用。你只需要知道IE需要不同格式的字体才能与其他浏览器兼容。请参见https://dev59.com/AXE95IYBdhLWcg3wp_kg。 - David Johnstone