我需要将文本垂直对齐到顶部或底部或中心。有用于水平对齐的 FormattedText.TextAlignment 属性,但没有用于垂直对齐的属性。
为了弄清楚谷歌不明白什么:我对旋转文本以垂直流动不感兴趣。我知道该怎么做。我只想让(可能是多行)文本在中心点上方或下方或中间对齐。
有人知道该怎么做吗?
我需要将文本垂直对齐到顶部或底部或中心。有用于水平对齐的 FormattedText.TextAlignment 属性,但没有用于垂直对齐的属性。
为了弄清楚谷歌不明白什么:我对旋转文本以垂直流动不感兴趣。我知道该怎么做。我只想让(可能是多行)文本在中心点上方或下方或中间对齐。
有人知道该怎么做吗?
如果您使用的是DrawingContext.DrawText
method ,则必须指定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);