Set VerticalAlignment in DrawText

纵饮孤独 提交于 2019-12-01 18:00:21

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

Last version of FormattedText with 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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!