我正在使用System.Windows.Media.FormattedText进行一些底层渲染(特别是尝试以印刷上令人愉悦的方式渲染数学方程式)。为此,我正在使用的文本块上的精确度量至关重要。

我正在创建几个FormattedText对象,并在最低渲染级别使用它们。问题是,如果其中任何一个包含尾随空格,则在计算FormattedText.Width属性时不会考虑该空格。例如,如果我写:

double w1 = new FormattedText ("Hello", ...).Width;
double w2 = new FormattedText ("Hello    ", ...).Width;


w1和w2相同。前导空间已正确测量。如何强制FormattedText也测量这些尾随空格?

最佳答案

从使用Width属性更改为使用WidthIncludingTrailingWhitespace属性。

double w1 = new FormattedText ("Hello", ...).WidthIncludingTrailingWhitespace;
double w2 = new FormattedText ("Hello    ", ...).WidthIncludingTrailingWhitespace;


使用此代码,您应该看到两个不同的宽度值。

关于c# - FormattedText Width属性不考虑尾随空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2028765/

10-10 21:46