Set VerticalAlignment to DrawText

I use DrawTextfor drawing FormattedTexton Visual Layer. Now I use the code below to define formatted text, and I can set TextAlignmentto Center. But what about VerticalAlignment? As you can see in the image below, the center of the text is not at the center point , which is displayed with a red dot here.

The part where I define 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 draw the text:

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

Here is the final result:

enter image description here

I want the middle of the text to be in the center of the circle.

+4
source share
2 answers

. , . . . .

, a FormattedText , VerticalAlignment, . Height . :

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

Here is the result

+6

FormattedText PixelsPerDip.

Typeface typeFace = new Typeface(new FontFamily("Segoe UI"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
FormattedText formattedText = new FormattedText("Bodrum Bodrum", CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeFace, 14, Brushes.Black, VisualTreeHelper.GetDpi(this).PixelsPerDip);

Point textPoint = new Point(100, 100);
drawingContext.DrawText(formattedText, textPoint);
0

Source: https://habr.com/ru/post/1534379/


All Articles