代码之家  ›  专栏  ›  技术社区  ›  T1960CT

模板中的Golang ttf字体

  •  0
  • T1960CT  · 技术社区  · 6 年前

    我试图得到一个TTF字体工作在一个golang模板,但它不会呈现字体。它显示为普通的新罗马时代。我可以使用标准字体系列字体(例如verdana或“helvetica”)更改字体,但我不能导入TTF。

    我能找到的关于TTF字体的所有信息都是向图像中添加文本的库,但我想更改web字体。我怎样才能做到这一点?

    项目结构为

    • /html\u模板/portal.html
    • 主菜单.go

    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>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Evgeny A. Mamonov    6 年前

    您需要处理静态文件,将其添加到main func并将url设置为 /static/Comfortaa-Regular.ttf 在模板中

    //Create MUX Routing for static
    fs := http.FileServer(http.Dir("./static"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))
    

    这是完整的工作代码

    package main
    
    import (
        "net/http"
        "text/template"
    )
    
    type Portal struct{
        Title string
    }
    
    func main(){
        //Create MUX Routing handlers
        http.HandleFunc("/", portal)
    
        //Create MUX Routing for static
        fs := http.FileServer(http.Dir("./static"))
        http.Handle("/static/", http.StripPrefix("/static/", fs))
    
        //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)
    }
    

    <head>
        <title>{{ .Title }}</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
        <style>
            @font-face {
                font-family: 'comfortaaRegular';
                src: url('/static/Comfortaa-Regular.ttf');
                src: local('comfortaaRegular'),
                     local('Comfortaa-Regular.ttf'),
                     url('/static/Comfortaa-Regular.ttf') format('truetype'),
            }
            body{ font-family: 'comfortaaRegular' }
        </style>
    </head>
    <body>
        <p>test</p>
    </body>
    </html>
    
    推荐文章