text.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package canvas
  2. import (
  3. "image/color"
  4. "fyne.io/fyne/v2"
  5. )
  6. // Declare conformity with CanvasObject interface
  7. var _ fyne.CanvasObject = (*Text)(nil)
  8. // Text describes a text primitive in a Fyne canvas.
  9. // A text object can have a style set which will apply to the whole string.
  10. // No formatting or text parsing will be performed
  11. type Text struct {
  12. baseObject
  13. Alignment fyne.TextAlign // The alignment of the text content
  14. Color color.Color // The main text draw color
  15. Text string // The string content of this Text
  16. TextSize float32 // Size of the text - if the Canvas scale is 1.0 this will be equivalent to point size
  17. TextStyle fyne.TextStyle // The style of the text content
  18. }
  19. // Hide will set this text to not be visible
  20. func (t *Text) Hide() {
  21. t.baseObject.Hide()
  22. repaint(t)
  23. }
  24. // MinSize returns the minimum size of this text object based on its font size and content.
  25. // This is normally determined by the render implementation.
  26. func (t *Text) MinSize() fyne.Size {
  27. return fyne.MeasureText(t.Text, t.TextSize, t.TextStyle)
  28. }
  29. // Move the text to a new position, relative to its parent / canvas
  30. func (t *Text) Move(pos fyne.Position) {
  31. t.baseObject.Move(pos)
  32. repaint(t)
  33. }
  34. // Resize on a text updates the new size of this object, which may not result in a visual change, depending on alignment.
  35. func (t *Text) Resize(s fyne.Size) {
  36. if s == t.Size() {
  37. return
  38. }
  39. t.baseObject.Resize(s)
  40. Refresh(t)
  41. }
  42. // SetMinSize has no effect as the smallest size this canvas object can be is based on its font size and content.
  43. func (t *Text) SetMinSize(fyne.Size) {
  44. // no-op
  45. }
  46. // Refresh causes this text to be redrawn with its configured state.
  47. func (t *Text) Refresh() {
  48. Refresh(t)
  49. }
  50. // NewText returns a new Text implementation
  51. func NewText(text string, color color.Color) *Text {
  52. size := float32(0)
  53. if fyne.CurrentApp() != nil { // nil app possible if app not started
  54. size = fyne.CurrentApp().Settings().Theme().Size("text") // manually name the size to avoid import loop
  55. }
  56. return &Text{
  57. Color: color,
  58. Text: text,
  59. TextSize: size,
  60. }
  61. }