Fyne setting Chinese

Fyne does not support Chinese by default (the version I use is 1.3.2). Therefore, it is necessary to introduce Chinese fonts

Create the init method in the go file where the main method is located

func init() {
	//设置中文字体
	os.Setenv("FYNE_FONT","Alibaba-PuHuiTi-Medium.ttf")
}

Yes, but this method does not put the font into the binary file after packaging, that is, put the packaged file in another location, and the Chinese will display abnormally.

So how to use the fonts provided by the system?

Find a go library on github

github

 

By introducing this library, you can realize the method of searching the system font library. At this time, you can import the corresponding font library. However, at present, the library can only find the TTF font library, and the TTC font library cannot be found. The operating system I use is win10, and Microsoft Ya The font format of Hei is TTC, so I used italics and bold, and you need to check it in the font settings of the operating system.

2020/810 Correction: ISSUE is mentioned to the author, and the font library in ttc format is now supported

Find the corresponding font and set the environment variable with code

func init() {
	fontPaths := findfont.List()
	for _, path := range fontPaths {
		fmt.Println(path)
		//楷体:simkai.ttf
		//黑体:simhei.ttf
		if strings.Contains(path, "simkai.ttf") {
			os.Setenv("FYNE_FONT", path)
			break
		}
	}
	fmt.Println("=============")
}

Don't forget to use at the end of the main method

os.Unsetenv("FYNE_FONT")

cancel environment variable

Guess you like

Origin blog.csdn.net/kanyun123/article/details/107784660