问题
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