亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

模板中的 Golang ttf 字體

模板中的 Golang ttf 字體

Go
偶然的你 2023-04-10 10:37:54
我試圖讓 TTF 字體在 golang 模板中工作,但它不會呈現字體。它顯示為常規的 Times New Roman。我可以使用標準字體系列字體(ex verdana 或 'helvetica')更改字體,但我無法導入 TTF。關于 TTF 字體,我似乎只能找到用于向圖像添加文本的庫,但我想更改網絡字體。我怎樣才能做到這一點?項目結構是/html_templates/portal.html/html_teplates/Comfortaa-Regular.ttf主程序這是相關的 golang 代碼:
查看完整描述

1 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

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>


查看完整回答
反對 回復 2023-04-10
  • 1 回答
  • 0 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號