【问题标题】:Get "ascent" LineHeight from Font从字体获取“上升” LineHeight
【发布时间】:2011-09-21 14:54:04
【问题描述】:

如何在给定 FontSize 的情况下获取字体的 LineHeight?似乎它因字体而异,不一定与FontSize有关。我将 BlockLineHeight 用于 LineStackingStrategy。

澄清。我知道有一些方法可以确定总行高。在这种情况下,我正在寻找从基线到字体顶部的高度(因此减去 p 的尾部等)

如上图所示。我想要上升。

【问题讨论】:

标签: c# wpf


【解决方案1】:

如果您使用Graphics 对象来绘制并引用,那么您可以这样做。

Font myFont = new Font("Verdana", 15);
SizeF fontSize = e.Graphics.MeasureString("my text", myFont);

这将告诉你字符串的高度和宽度。您可以将其用于单行以测试行高。

或者在这里回答:How to calculate font height in WPF? 您可以通过一些简单的计算轻松计算出线高。

【讨论】:

  • Graphics 对象是什么?
  • 当为控件触发 Paint 事件时,它会得到PaintEventArgs e。 e 包含一个 Graphics 对象。
【解决方案2】:
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);

来自: http://msdn.microsoft.com/en-us/library/xwf9s90b.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多