CanvasView. Text

To display text in specified coordinates, two functions are used

FillText(x, y float64, text string)
StrokeText(x, y float64, text string)

The StrokeText function draws the outline of the text, FillText draws the text itself.

The horizontal alignment of the text relative to the specified coordinates is set using the function

SetTextAlign(align int)

where align can be one of the following values:

Value Constant Alignment
0 LeftAlign The specified point is the leftmost point of the text
1 RightAlign The specified point is the rightmost point of the text
2 CenterAlign The text is centered on the specified point
3 StartAlign If the text is displayed from left to right, then the text output is equivalent to LeftAlign, otherwise RightAlign
4 EndAlign If the text is displayed from left to right, then the text output is equivalent to RightAlign, otherwise LeftAlign

The vertical alignment of the text relative to the specified coordinates is set using the functionSetTextBaseline(baseline int)

where baseline can be one of the following values:

Value Constant Alignment
0 AlphabeticBaseline Relatively normal baseline of text
1 TopBaseline Relative to the top border of the text
2 MiddleBaseline About the middle of the text
3 BottomBaseline To the bottom of the text
4 HangingBaseline Relative to the dangling baseline of the text (used in Tibetan and other Indian scripts)
5 IdeographicBaseline Relative to the ideographic baseline of the text

An ideographic baseline is the bottom of a character display if the main character isoutside the alphabet baseline (Used in Chinese, Japanese, and Korean fonts).

To set the font parameters of the displayed text, use the functions

SetFont(name string, size SizeUnit)
SetFontWithParams(name string, size SizeUnit, params FontParams)

where FontParams is defined as

type FontParams struct {
	// Italic - if true then a font is italic
	Italic bool
	// SmallCaps - if true then a font uses small-caps glyphs
	SmallCaps bool
	// Weight - a font weight. Valid values: 0…9, there
	//   0 - a weight does not specify;
	//   1 - a minimal weight;
	//   4 - a normal weight;
	//   7 - a bold weight;
	//   9 - a maximal weight.
	Weight int
	// LineHeight - the height (relative to the font size of the element itself) of a line box.
	LineHeight SizeUnit
}

The TextWidth function allows you to find out the width of the displayed text in pixels

TextWidth(text string, fontName string, fontSize SizeUnit) float64