我需要将文本垂直对齐到顶部或底部或中心。有 FormattedText.TextAlignment 属性用于水平对齐,但没有用于垂直对齐。
为了澄清 Google 不理解的内容:我对旋转文本以垂直流动不感兴趣。我知道该怎么做。我只想让(可能是多行)文本在中心点上方或下方或中间对齐。
有人知道怎么做吗?
最佳答案
如果您使用的是 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);
关于wpf - 与 DrawingContext.DrawText 垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25308612/