Vertical alignment with DrawingContext.DrawText [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-02 01:08:58
Roel van Westerop

If you're using the DrawingContext.DrawText method , you have to specify a Point where the text will be positioned. That means you have to calculate the alignment yourself.

Small example, assuming centerPoint is the point you want your text to align to and drawingContext is the DrawingContext, centering the text above that point:

Typeface typeface = new Typeface(new FontFamily("Segoe UI"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
FormattedText formattedText = new FormattedText("Text to render", CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeFace, 16, Brushes.Black);

Point textLocation = new Point(centerPoint.X - formattedText.WidthIncludingTrailingWhitespace / 2, center.Y - formattedText.Height);
drawingContext.DrawText(formattedText, textLocation);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!