WebGraphics.TextAlignment

From Xojo Documentation

Property (As Integer )
aWebGraphics.TextAlignment = newIntegerValue
or
IntegerValue = aWebGraphics.TextAlignment

New in 2013r1

Supported for all project types and targets.

Sets the text alignment of text created with DrawString. There are 3 Constants for setting this value:

  • WebGraphics.TextAlignmentLeft = 0
  • WebGraphics.TextAlignmentCenter = 1
  • WebGraphics.TextAlignmentRight = 2

Examples

This code centers the text in the drawing area:

Var txt As String = "Hello World"
Var y As Integer = 100
Var alignX As Integer = g.Width / 2
g.TextAlignment = WebGraphics.TextAlignmentCenter
g.DrawString(txt, alignX, y)

Show left-aligned, right-aligned and centered text, both within a Rect and within the entire Canvas. Be sue the Canvas is locked to all page edges so that it is large enough:

g.DrawRect(0, 10, 200, 100)

g.TextAlignment = WebGraphics.TextAlignmentLeft
g.DrawString("Left-aligned (Box)", 5, 20) ' in box
g.DrawString("Left-aligned (Canvas)", 5, 200, g.Width) ' in canvas

g.TextAlignment = WebGraphics.TextAlignmentRight
g.DrawString("Right-aligned (Box)", 195, 40) ' in box
g.DrawString("Right-aligned (Canvas)", g.Width, 250, g.Width) ' in canvas

g.TextAlignment = WebGraphics.TextAlignmentCenter
g.Drawstring("Centered (Box)", 100, 60) ' in box
g.DrawString("Centered (Canvas)", g.Width / 2, 300, g.Width) ' in canvas

This code splits a number into two parts so you can do decimal alignment:

Var c As Currency = 15.32
Var parts() As String = Split(Str(c, "0.00"), ".")
Var y As Integer = 100
Var alignX As Integer = g.Width/2
g.TextAlignment = WebGraphics.TextAlignmentRight
g.DrawString(parts(0) + ".", alignX, y)
g.TextAlignment = WebGraphics.TextAlignmentLeft
g.DrawString(parts(1), alignX, y)