|
|
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
|