【发布时间】:2011-09-21 14:54:04
【问题描述】:
如何在给定 FontSize 的情况下获取字体的 LineHeight?似乎它因字体而异,不一定与FontSize有关。我将 BlockLineHeight 用于 LineStackingStrategy。
澄清。我知道有一些方法可以确定总行高。在这种情况下,我正在寻找从基线到字体顶部的高度(因此减去 p 的尾部等)
如上图所示。我想要上升。
【问题讨论】:
如何在给定 FontSize 的情况下获取字体的 LineHeight?似乎它因字体而异,不一定与FontSize有关。我将 BlockLineHeight 用于 LineStackingStrategy。
澄清。我知道有一些方法可以确定总行高。在这种情况下,我正在寻找从基线到字体顶部的高度(因此减去 p 的尾部等)
如上图所示。我想要上升。
【问题讨论】:
如果您使用Graphics 对象来绘制并引用,那么您可以这样做。
Font myFont = new Font("Verdana", 15);
SizeF fontSize = e.Graphics.MeasureString("my text", myFont);
这将告诉你字符串的高度和宽度。您可以将其用于单行以测试行高。
或者在这里回答:How to calculate font height in WPF? 您可以通过一些简单的计算轻松计算出线高。
【讨论】:
PaintEventArgs e。 e 包含一个 Graphics 对象。
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(fontFamily, 16, FontStyle.Regular, GraphicsUnit.Pixel);
ascent = fontFamily.GetCellAscent(FontStyle.Regular);
ascentPixel = font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);
【讨论】: