与DrawingContext.DrawText的垂直对齐 [英] Vertical alignment with DrawingContext.DrawText

查看:339
本文介绍了与DrawingContext.DrawText的垂直对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使文本垂直对齐顶部,底部或中心。有用于水平对齐的FormattedText.TextAlignment属性,但没有用于垂直对齐的属性。

I need to align my text vertically to either the top or bottom or center. There's the FormattedText.TextAlignment property for horizontal alignment, but nothing for vertical alignment.

要弄清楚Google不了解的内容:我对将文本旋转到垂直流动。我知道该怎么做。我只想让(可能是多行)文本在中心点上方或下方或中间对齐。

To make it clear what Google doesn't understand: I am not interested in rotating text to flow vertically. I know how to do that. I just want to have (possibly multi-line) text to align above or below the centre point or in the middle of it.

有人知道怎么做吗?

推荐答案

如果您使用的是 DrawingContext.DrawText 方法,以指定 Point 的位置。

If you're using the DrawingContext.DrawText method , you have to specify a Point where the text will be positioned. That means you have to calculate the alignment yourself.

小例子,假设 centerPoint 是您想要文本的点并与 drawingContext 对齐的是 DrawingContext ,使文本在该点上方居中:

Small example, assuming centerPoint is the point you want your text to align to and drawingContext is the DrawingContext, centering the text above that point:

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);

这篇关于与DrawingContext.DrawText的垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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