在DrawText中设置VerticalAlignment [英] Set VerticalAlignment in DrawText

查看:146
本文介绍了在DrawText中设置VerticalAlignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DrawText在Visual Layer上绘制FormattedText.现在,我使用下面的代码定义带格式的文本,并且可以将TextAlignment设置为Center.但是VerticalAlignment呢?正如您在图像下方看到的那样,文本中心不在中心点上,该点在此处用红点显示.

I'm using DrawText to draw FormattedText on Visual Layer. Now I'm using the code below to define the formatted text and I'm able to set the TextAlignment to Center. But what about the VerticalAlignment? As you can see in the image below the center of the text is not on the centerpoint which is shown with a red dot here.

我要定义FormattedText的部分:

The part where I'm defining the 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);

这是最终结果:

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

I want the middle of the text to lie on the center of the circle.

推荐答案

好吧,看来我能够解决这个问题.这并不难.我将答案发布在这里,以备将来参考.而且它也可能对其他人有帮助.

Well Ok it seems I was able to solve this. It is not that hard. I'll post the answer here for future reference. And It might help other people too.

看来FormattedText并不存在VerticalAlignment这样的东西,因此我们需要自己计算和定位.因为我们可以获得格式化文本的Height属性.我们可以像这样轻松对齐文本:

As it seems there is not such a thing as VerticalAlignment for a FormattedText so we need to calculate and position it ourselves. Since we can get the Height property of the formatted text. We can easily align the text like this:

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

这篇关于在DrawText中设置VerticalAlignment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆