在DrawText中设置VerticalAlignment

时间:2014-04-01 08:13:41

标签: c# wpf vertical-alignment drawtext

我使用DrawText在可视图层上绘制FormattedText。现在,我使用下面的代码来定义格式化文本,并且我可以将TextAlignment设置为Center。但是VerticalAlignment呢?如下图所示,文本中心不在中心点上,此处显示红点。

我定义FormattedText的部分:

var ft = new FormattedText("A",
    CultureInfo.GetCultureInfo("en-us"),
    FlowDirection.LeftToRight,
    new Typeface("Verdana"),
    36, Brushes.Yellow);

ft.TextAlignment = TextAlignment.Center;

我正在绘制文字的部分:

var centerpoint = new Point(0,0);
dc.DrawText(ft, centerpoint);

以下是最终结果:

enter image description here

我希望文本的中间位于圆圈的中心。

2 个答案:

答案 0 :(得分:6)

好吧好像我能够解决这个问题。这并不难。我将在此处发布答案以供将来参考。它也可以帮助其他人。

因为VerticalAlignment似乎不存在FormattedText这样的事情,所以我们需要自己计算和定位它。因为我们可以获得格式化文本的Height属性。我们可以轻松地对齐文本:

dc.DrawText(ft, new Point(centerpoint.X, centerpoint.Y- ft.Height/2));

Here is the result

答案 1 :(得分:0)

带有FormattedText的{​​{1}}的最新版本

PixelsPerDip.