【发布时间】:2019-02-26 23:31:37
【问题描述】:
我正在尝试让 TTF 字体在 golang 模板中工作,但它不会呈现字体。它显示为常规的 Times New Roman。我可以使用标准字体系列字体(例如 verdana 或“helvetica”)更改字体,但我无法导入 TTF。
关于 TTF 字体,我似乎只能找到用于向图像添加文本的库,但我想更改网络字体。我怎样才能做到这一点?
项目结构是
- /html_templates/portal.html
- /html_teplates/Comfortaa-Regular.ttf
- main.go
这里是相关的golang代码:
import (
"fmt"
"net/http"
"text/template"
)
type Portal struct{
Title string
}
func main(){
//Create MUX Routing handlers
http.HandleFunc("/", portal)
//Start WebServer
if err := http.ListenAndServe(":1234", nil); err != nil{ panic(err) }
}
func portal(w http.ResponseWriter, r *http.Request){
//Create template
tmpl, _ := template.ParseFiles("./html_templates/portal.html")
//Populate struct
portal := Portal{
Title: "title",
}
//Execute template with struct data
tmpl.Execute(w, portal)
}
以及相关的HTML:
<html>
<head>
<title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
@font-face {
font-family: 'comfortaaRegular';
src: url('Comfortaa-Regular.ttf');
src: local('comfortaaRegular'),
local('Comfortaa-Regular.ttf'),
url('Comfortaa-Regular.ttf') format('truetype'),
}
body{ font-family: 'comfortaaRegular' }
</style>
</head>
<body>
<p>test/p>
</body>
</html>
【问题讨论】:
-
您是否尝试过按照 Google 提供的示例进行操作?这包括您缺少的对
https://fonts.googleapis.com/css?family=[family-name]的特定引用和更直接的font-family=[family-name]引用。 [链接:developers.google.com/fonts/docs/getting_started]