与DrawingContext.DrawText垂直对齐

ygo*_*goe 2 wpf text alignment drawingcontext

我需要将文本垂直对齐到顶部或底部或中心.有水平对齐的FormattedText.TextAlignment属性,但没有垂直对齐的属性.

要明确Google不理解的内容:我对旋转文本垂直流动不感兴趣.我知道该怎么做.我只想让(可能是多行)文本在中心点的上方或下方或中间对齐.

谁知道怎么做?

Roe*_*rop 8

如果您正在使用该DrawingContext.DrawText方法,则必须指定Point文本的位置.这意味着你必须自己计算对齐.

小例子,假设centerPoint您希望文本与之对齐,并且drawingContext是以该DrawingContext点为中心的文本:

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);
Run Code Online (Sandbox Code Playgroud)