Fyne设置中文,解决中文乱码

Fyne默认不支持中文

 

 

 

解决:
先安装go-findfont 地址https://github.com/flopp/go-findfont.git,控制台执行

go get -u github.com/flopp/go-findfont

粘贴这段代码

复制代码
func init() {
   fontPath, err := findfont.Find("SIMYOU.TTF")
   if err != nil {
      panic(err)
   }
   fmt.Printf("Found 'arial.ttf' in '%s'\n", fontPath)

   // load the font with the freetype library
   // 原作者使用的ioutil.ReadFile已经弃用
   fontData, err := os.ReadFile(fontPath)
   if err != nil {
      panic(err)
   }
   _, err = truetype.Parse(fontData)
   if err != nil {
      panic(err)
   }
   os.Setenv("FYNE_FONT", fontPath)
}
复制代码

到项目根目录下新建一个文件夹用来存放字体文件

 

 Windows电脑搜索C:\Windows\Fonts

 

 

找到喜欢的字体复制粘贴到新建的文件夹
替换"喜欢的字体"为文件名

测试
复制代码
func main() {
    // use the font...
    MyApp := app.New()
    c := MyApp.NewWindow("fyne支持中文")
    labels := widget.NewLabel("中文看看行不行")
    c.SetContent(labels)
    c.Resize(fyne.NewSize(600, 600))
    c.ShowAndRun()
}
复制代码

结果

 

posted @   VCCICCV  阅读(4792)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示