从零开始的Fyne(七):绘制与动画、容器、组件、收纳样式(没意思)
绘制与动画
矩形/Rectangle
canvas.Rectangleis the simplest canvas object in Fyne. It displays a block of the specified colour. You can also set the colour using theFillColorfield.
In this example the rectangle fills the window as it is the only content element.
package main
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Rectangle")
rect := canvas.NewRectangle(color.White)
w.SetContent(rect)
w.Resize(fyne.NewSize(150, 100))
w.ShowAndRun()
}Other
fyne.CanvasObjecttypes have more configuration, let us look next atcanvas.Text.
文本/Text
canvas.Textis used for all text rendering within Fyne. It is created by specifying the text and colour for the text. Text is rendered using the default font, specified by the current theme.
The text object allows certain configuration like the
AlignmentandTextStylefield. as illustrated in the example here. To use a monospaced font instead you can specifyfyne.TextStyle{Monospace: true}.
package main
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Text")
text := canvas.NewText("Text Object", color.White)
text.Alignment = fyne.TextAlignTrailing
text.TextStyle = fyne.TextStyle{Italic: true}
w.SetContent(text)
w.ShowAndRun()
}It is possible to use an alternative font by specifying a
FYNE_FONTenvironment variable. Use this to set a.ttffile to use instead of the one provided in the Fyne toolkit or the current theme.