Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set VerticalAlignment in DrawText

I'm using DrawText to draw FormattedText on Visual Layer. Now I'm using the code below to define the formatted text and I'm able to set the TextAlignment to Center. But what about the VerticalAlignment? As you can see in the image below the center of the text is not on the centerpoint which is shown with a red dot here.

The part where I'm defining the FormattedText :

var ft = new FormattedText("A",
    CultureInfo.GetCultureInfo("en-us"),
    FlowDirection.LeftToRight,
    new Typeface("Verdana"),
    36, Brushes.Yellow);

ft.TextAlignment = TextAlignment.Center;

The part where I'm drawing the text:

var centerpoint = new Point(0,0);
dc.DrawText(ft, centerpoint);

Here is final result:

enter image description here

I want the middle of the text to lie on the center of the circle.

like image 874
Vahid Avatar asked Apr 01 '14 08:04

Vahid


1 Answers

Well Ok it seems I was able to solve this. It is not that hard. I'll post the answer here for future reference. And It might help other people too.

As it seems there is not such a thing as VerticalAlignment for a FormattedText so we need to calculate and position it ourselves. Since we can get the Height property of the formatted text. We can easily align the text like this:

dc.DrawText(ft, new Point(centerpoint.X, centerpoint.Y- ft.Height/2));

Here is the result

like image 105
Vahid Avatar answered Sep 22 '22 08:09

Vahid