支持中文字体

This commit is contained in:
zhaocy 2022-07-26 18:39:09 +08:00
parent aa47c50629
commit d577bdfc2d
2 changed files with 75 additions and 3 deletions

Binary file not shown.

View File

@ -1,16 +1,24 @@
package main
import (
"errors"
"image/color"
"log"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func main() {
t := &myTheme{}
t.SetFonts("./assets/font/Consolas-with-Yahei Nerd Font.ttf", "")
a := app.NewWithID("dreamfactory")
a.Settings().SetTheme(t)
win := a.NewWindow("昊天锤")
win.Resize(fyne.NewSize(1366, 768))
win.CenterOnScreen()
@ -45,9 +53,73 @@ func ShowLoginFormDialog(win fyne.Window) {
log.Println("Please Authenticate", username.Text, rememberText)
if username.Text != "admin" {
// widget.NewButton("Error", func() {
// err := errors.New("account 错误")
// dialog.ShowError(err, win)
// }),
err := errors.New("account 错误")
dialog.ShowError(err, win)
// })
}
}, win)
}
type myTheme struct {
regular, bold, italic, boldItalic, monospace fyne.Resource
}
func (t *myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
return theme.DefaultTheme().Color(name, variant)
}
func (t *myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
return theme.DefaultTheme().Icon(name)
}
func (m *myTheme) Font(style fyne.TextStyle) fyne.Resource {
if style.Monospace {
return m.monospace
}
if style.Bold {
if style.Italic {
return m.boldItalic
}
return m.bold
}
if style.Italic {
return m.italic
}
return m.regular
}
func (m *myTheme) Size(name fyne.ThemeSizeName) float32 {
return theme.DefaultTheme().Size(name)
}
func (t *myTheme) SetFonts(regularFontPath string, monoFontPath string) {
t.regular = theme.TextFont()
t.bold = theme.TextBoldFont()
t.italic = theme.TextItalicFont()
t.boldItalic = theme.TextBoldItalicFont()
t.monospace = theme.TextMonospaceFont()
if regularFontPath != "" {
t.regular = loadCustomFont(regularFontPath, "Regular", t.regular)
t.bold = loadCustomFont(regularFontPath, "Bold", t.bold)
t.italic = loadCustomFont(regularFontPath, "Italic", t.italic)
t.boldItalic = loadCustomFont(regularFontPath, "BoldItalic", t.boldItalic)
}
if monoFontPath != "" {
t.monospace = loadCustomFont(monoFontPath, "Regular", t.monospace)
} else {
t.monospace = t.regular
}
}
func loadCustomFont(env, variant string, fallback fyne.Resource) fyne.Resource {
variantPath := strings.Replace(env, "Regular", variant, -1)
res, err := fyne.LoadResourceFromPath(variantPath)
if err != nil {
fyne.LogError("Error loading specified font", err)
return fallback
}
return res
}