How to calculate font height in WPF?
Solution 1
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).
Solution 2
Use System.Windows.Media.FormattedText
class.
Example:
FormattedText ft = new FormattedText("Quick Brown Fox Jumps Over A Lazy Dog.",
CultureInfo.CurrentCulture,
CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight,
new Typeface("Verdana"),
9,
new SolidColorBrush(Colors.White)
Double maxHeight = ft.MaxTextHeight;
Related videos on Youtube
Comments
-
Tim Lloyd about 2 years
For a
FontFamily
how do I programatically retrieve/calculate the maximum height range for that font at a particularFontSize
?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.-
marbel82 over 5 yearsTake a look at how TextBox calculates the height of the line TextBox.GetLineHeight()
-
-
Tim Lloyd over 13 yearsDo you have an example of using this to say work out the height range for Verdana at 9pt? The question is not to work out the height of sample text, but for a FontFamily.
-
Tim Lloyd over 13 years@decyclone But isn't that working out the maximum height of the sample text i.e. "Quick Brown Fox Jumps Over A Lazy Dog."?
-
decyclone over 13 yearsWhat is the possibility of characters being used other than all the alphabets? For calculating Height, letter
H
in caps should be enough. -
Tim Lloyd over 13 years@decyclone That is not even an entire alphabet - that's just some English word characters and doesn't take into consideration scientific symbols, etc. It's not that simplistic unfortunately. I want something that will work against the whole FontFamily, not just sample text.
-
Tim Lloyd over 13 years@decyclone French for example has capital letters with ascenders e.g. 'È'. That's taller than a capital H e.g. ÈHÈ.
-
decyclone over 13 years@chibacity: That answer my question
What is the possibility of characters being used other than all the alphabets?
... I agree with you that this code won't help you with problem you mentioned. -
Tim Lloyd over 13 years@decyclone Yes, basing the measurement on sample text is not going to work for a variety of reasons - it's quite a thorny issue. Thanks for having a go though. :)
-
Tom Hunter almost 12 yearsWhy do you use
Math.Ceiling()
? I'm using Courier New size 12 andtextbox.FontSize * textBox.FontFamily.LineSpacing
gives 13.59375. -
Tim Lloyd almost 12 yearsNo reason other than to work with whole numbers to reduce the chance that positioning other elements will not become blurry. If using the exact figure works for you, go for it.
-
Conrad about 11 yearsBetter to use
UseLayoutRounding="True"
than to lop off the decimal portion of the fontHeight, if reducing anti-aliasing is your goal. -
marbel82 over 5 yearsYou shouldn't use Math.Ceiling() when calculating the height of the TextBox line, because it depends on TextFormattingMode. See how TextBox.GetLineHeight() calculates that value.