如何计算WPF中的字体高度? [英] How to calculate font height in WPF?
问题描述
对于 FontFamily
如何在特定的 FontSize
下以编程方式检索/计算该字体的最大高度范围。 ?
我需要一个值来设置在指定的 FontSize
处显示字体的文本块的高度。这必须以编程方式进行。
我需要一个考虑上行和下行等的值。
为了澄清,我需要整个 FontFamily $ c $的最大高度范围
字体的最大高度范围可以使用其<线性空间属性,这是一个比例数字为字体。这可以用来给出一个行高度,可以容纳特定大小的字体的所有字形。
FontFamily fontFamily = new FontFamily(Segoe UI);
double fontDpiSize = 16;
double fontHeight = Math.Ceiling(fontDpiSize * fontFamily.LineSpacing);
结果:
22.0
这个数字将包含少量的
For a FontFamily
how do I programatically retrieve/calculate the maximum height range for that font at a particular FontSize
?
I need a value to set the height of a textblock that will display the font at the specified FontSize
- this has to be carried out programatically.
I need a value that will take into consideration ascenders and descenders, etc.
Update
To clarify, I need the maximum height range for the entire FontFamily
, not the height of some sample text in that font. I do not know what the text will be in advance.
The maximum height range for a font can be calculated using its LineSpacing property which is a proportional figure for the font. This can be used to give a line height which can accommodate all glyphs for that font at a particular size.
FontFamily fontFamily = new FontFamily("Segoe UI");
double fontDpiSize = 16;
double fontHeight = Math.Ceiling(fontDpiSize * fontFamily.LineSpacing);
Result:
22.0
This figure will contain a small amount of leading which is desirable when needing a height for rows of text (so that ascenders and descenders from adjacent rows of text have spacing).
这篇关于如何计算WPF中的字体高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!