Vertical alignment with DrawingContext.DrawText [duplicate]

二次信任 提交于 2019-12-20 03:27:16

问题


I need to align my text vertically to either the top or bottom or center. There's the FormattedText.TextAlignment property for horizontal alignment, but nothing for vertical alignment.

To make it clear what Google doesn't understand: I am not interested in rotating text to flow vertically. I know how to do that. I just want to have (possibly multi-line) text to align above or below the centre point or in the middle of it.

Anybody knows how to do that?


回答1:


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


来源:https://stackoverflow.com/questions/25308612/vertical-alignment-with-drawingcontext-drawtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!