zntg/ui/label.go

41 lines
882 B
Go
Raw Permalink Normal View History

package ui
import (
"opslag.de/schobers/geom"
)
type Label struct {
ControlBase
Text string
size desiredSizeCache
}
func BuildLabel(text string, fn func(*Label)) *Label {
var l = &Label{Text: text}
if fn != nil {
fn(l)
}
return l
}
func (l *Label) DesiredSize(ctx Context) geom.PointF32 {
var fontName = l.FontName(ctx)
return l.size.Update(ctx, fontName+l.Text, func(ctx Context) geom.PointF32 {
var font = ctx.Renderer().Font(fontName)
var width = font.WidthOf(l.Text)
var height = font.Height()
var pad = ctx.Style().Dimensions.TextPadding
return geom.PtF32(width+pad*2, height+pad*2)
})
}
func (l *Label) Render(ctx Context) {
l.RenderBackground(ctx)
var c = l.FontColor(ctx)
var f = l.FontName(ctx)
var pad = ctx.Style().Dimensions.TextPadding
2019-06-24 18:58:14 +00:00
ctx.Renderer().TextAlign(l.bounds.Min.Add(geom.PtF32(pad, pad)), f, c, l.Text, l.TextAlignment)
}