How to calculate font height in WPF?

13,759

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).

enter image description here

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;
Share:
13,759

Related videos on Youtube

Tim Lloyd
Author by

Tim Lloyd

chibacity at gojisoft dot com

Updated on May 13, 2022

Comments

  • Tim Lloyd
    Tim Lloyd about 2 years

    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.

  • Tim Lloyd
    Tim Lloyd over 13 years
    Do 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
    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
    decyclone over 13 years
    What is the possibility of characters being used other than all the alphabets? For calculating Height, letter H in caps should be enough.
  • Tim Lloyd
    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
    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
    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
    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
    Tom Hunter almost 12 years
    Why do you use Math.Ceiling()? I'm using Courier New size 12 and textbox.FontSize * textBox.FontFamily.LineSpacing gives 13.59375.
  • Tim Lloyd
    Tim Lloyd almost 12 years
    No 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
    Conrad about 11 years
    Better to use UseLayoutRounding="True" than to lop off the decimal portion of the fontHeight, if reducing anti-aliasing is your goal.
  • marbel82
    marbel82 over 5 years
    You 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.