Gio中文显示方法

创建regular 包:

里面放data.go:,同时放上下载好的中文字体

package regular

import _ "embed"

//go:embed Alibaba-PuHuiTi-Regular.ttf
var TTF []byte

使用方法:

// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"image/color"
	"log"
	"os"

	"hello/regular"

	"gioui.org/app" // app contains Window handling.

	// gofont is used for loading the default font.
	"gioui.org/font/opentype"
	"gioui.org/io/system"       // system is used for system events (e.g. closing the window).
	"gioui.org/layout"          // layout is used for layouting widgets.
	"gioui.org/op"              // op is used for recording different operations.
	"gioui.org/text"            // text contains constants for text layouting.
	"gioui.org/unit"            // unit is used to define pixel-independent sizes
	"gioui.org/widget/material" // material contains material design widgets.
)

var (
	TitleColor = color.NRGBA{R: 127, G: 0, B: 0, A: 255}
)

func main() {
	// The ui loop is separated from the application window creation
	// such that it can be used for testing.
	ui := NewUI()

	go func() {
		w := app.NewWindow(
			// Set the window title.
			app.Title("Hello, Prod!"),
			// Set the size for the window.
			app.Size(unit.Dp(800), unit.Dp(400)),
		)
		if err := ui.Run(w); err != nil {
			log.Println(err)
			os.Exit(1)
		}
		os.Exit(0)
	}()

	app.Main()
}

// UI holds all of the application state.
type UI struct {
	// Theme is used to hold the fonts used throughout the application.
	Theme *material.Theme
}

// NewUI creates a new UI using the Go Fonts.
func NewUI() *UI {
	ui := &UI{}
	// Load the theme and fonts.

	font, err := opentype.Parse(regular.TTF)
	if err != nil {
		panic(err)
	}
	fonts := []text.FontFace{
		{Face: font},
	}

	ui.Theme = material.NewTheme(fonts)
	return ui
}

// Run handles window events and renders the application.
func (ui *UI) Run(w *app.Window) error {
	// ops will be used to encode different operations.
	var ops op.Ops

	// listen for events happening on the window.
	for e := range w.Events() {
		// detect the type of the event.
		switch e := e.(type) {
		// this is sent when the application should re-render.
		case system.FrameEvent:
			// gtx is used to pass around rendering and event information.
			gtx := layout.NewContext(&ops, e)
			// handle all UI logic.
			ui.Layout(gtx)
			// render and handle the operations from the UI.
			e.Frame(gtx.Ops)

		// this is sent when the application is closed.
		case system.DestroyEvent:
			return e.Err
		}
	}

	return nil
}

// Layout handles rendering and input.
func (ui *UI) Layout(gtx layout.Context) layout.Dimensions {
	return Title(ui.Theme, "Hello,你好!中华人民共和国").Layout(gtx)
}

// Title creates a center aligned H1.
func Title(th *material.Theme, caption string) material.LabelStyle {
	l := material.H1(th, caption)
	l.Color = TitleColor
	l.Alignment = text.Middle
	return l
}

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值