繁体   English   中英

在DrawText中设置VerticalAlignment

[英]Set VerticalAlignment in DrawText

我正在使用DrawText在Visual Layer上绘制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);

这是最终结果:

在此输入图像描述

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

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

因为看起来没有像FormattedText那样的VerticalAlignment ,所以我们需要自己计算和定位它。 因为我们可以获得格式化文本的Height属性。 我们可以轻松地将文本对齐如下:

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

这是结果

带有PixelsPerDip.FormattedText最新版本PixelsPerDip.

Typeface typeFace = new Typeface(new FontFamily("Segoe UI"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
FormattedText formattedText = new FormattedText("Bodrum Bodrum", CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeFace, 14, Brushes.Black, VisualTreeHelper.GetDpi(this).PixelsPerDip);

Point textPoint = new Point(100, 100);
drawingContext.DrawText(formattedText, textPoint);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM