Type Default Value Read Only Static Description
Double 0 No No The text horizontal alignment.
Notes

This property reflects the horizontal alignment of text within its rectangle. HAlign specifies the text rectangle alignment within the Canvas, whilst TextHAlign specifies the text alignment within it rectangle, if one has been set via the Canvas DrawText parameters.

You can specify a value ranging from zero to one:

  • 0 corresponds to left - the text is left aligned
  • 0.5 corresponds to middle - the text is centered
  • 1 corresponds to right - the text is right aligned.

If you do not specify a rectangle for the text, then this property will position the text in the Canvas, because when no rectangle is specified the entire Canvas rectangle is used. HAlign will have no effect in this case.

See Also

TextVAlign

Example

Here we experiment with text alignment:

  • The top text relies entirely on TextHAlign and TextValign for positioning. When no text position or size is specified, these parameters position the text on the Canvas. HAlign and VAlign have no effect because the text area is the entire Canvas.
  • The middle text sets the text size. This means the text rectangle is limited to the area identified by this size and the HAlign and VAlign properties are used to position such area. TextHAlign and TextValign position text within such area.
  • The bottom text specifies text size and position. HAlign and VAlign have therefore no use as the text area is fully positioned. TextHAlign and TextVAlign will still position the text within its area.

[C#]Canvas canvas = new Canvas(300, 200, new XColor(Color.Yellow));
DrawOptions drawOpts = new DrawOptions(canvas);
drawOpts.TextVAlign = 0;
drawOpts.TextHAlign = 0;
canvas.DrawText("Top Left", drawOpts);
drawOpts.TextHAlign = 1;
canvas.DrawText("Top Right", drawOpts);
drawOpts.TextHAlign = 0.5;
canvas.DrawText("Top Middle", drawOpts);
drawOpts.Reset(canvas);
XText lineInfo = canvas.GetTextMetrics("Middle", drawOpts);
double lineWidth = canvas.Width;
double lineHeight = lineInfo.LineHeight + 2;
drawOpts.VAlign = 0.5;
drawOpts.TextHAlign = 0;
canvas.DrawText("Middle Left", lineWidth, lineHeight, drawOpts);
drawOpts.TextHAlign = 1;
canvas.DrawText("Middle Right", lineWidth, lineHeight, drawOpts);
drawOpts.TextHAlign = 0.5;
canvas.DrawText("Middle Middle", lineWidth, lineHeight, drawOpts);
drawOpts.Reset(canvas);
double bottomPos = canvas.Height - lineHeight;
drawOpts.TextHAlign = 0;
canvas.DrawText("Bottom Left", new XRect(0, bottomPos, lineWidth, lineHeight), drawOpts);
drawOpts.TextHAlign = 1;
canvas.DrawText("Bottom Right", new XRect(0, bottomPos, lineWidth, lineHeight), drawOpts);
drawOpts.TextHAlign = 0.5;
canvas.DrawText("Bottom Middle", new XRect(0, bottomPos, lineWidth, lineHeight), drawOpts);
canvas.SaveAs(Server.MapPath("DrawOptions_TextAlign_102.gif"));


[Visual Basic]Dim canvas As New Canvas(300, 200, New XColor(Color.Yellow))
Dim drawOpts As New DrawOptions(canvas)
drawOpts.TextVAlign = 0
drawOpts.TextHAlign = 0
canvas.DrawText("Top Left", drawOpts)
drawOpts.TextHAlign = 1
canvas.DrawText("Top Right", drawOpts)
drawOpts.TextHAlign = 0.5
canvas.DrawText("Top Middle", drawOpts)
drawOpts.Reset(canvas)
Dim lineInfo As XText = canvas.GetTextMetrics("Middle", drawOpts)
Dim lineWidth As Double = canvas.Width
Dim lineHeight As Double = lineInfo.LineHeight + 2
drawOpts.VAlign = 0.5
drawOpts.TextHAlign = 0
canvas.DrawText("Middle Left", lineWidth, lineHeight, drawOpts)
drawOpts.TextHAlign = 1
canvas.DrawText("Middle Right", lineWidth, lineHeight, drawOpts)
drawOpts.TextHAlign = 0.5
canvas.DrawText("Middle Middle", lineWidth, lineHeight, drawOpts)
drawOpts.Reset(canvas)
Dim bottomPos As Double = canvas.Height - lineHeight
drawOpts.TextHAlign = 0
canvas.DrawText("Bottom Left", New XRect(0, bottomPos, lineWidth, lineHeight), drawOpts)
drawOpts.TextHAlign = 1
canvas.DrawText("Bottom Right", New XRect(0, bottomPos, lineWidth, lineHeight), drawOpts)
drawOpts.TextHAlign = 0.5
canvas.DrawText("Bottom Middle", New XRect(0, bottomPos, lineWidth, lineHeight), drawOpts)
canvas.SaveAs(Server.MapPath("DrawOptions_TextAlign_102.gif"))



DrawOptions_TextAlign_102.gif