Determine the Font & Point Size at Runtime When Using AppStylist

Jason Beres [Infragistics] / Saturday, February 28, 2009

At runtime, when checking the Font property of the UltraLabel, it returns the default font that the control renders with.  When the text actually drawn on the control, it is using the correct font specified in the AppStylist ISL file.  The result of this is that the measurements are incorrect, and the text gets clipped because the window is sized incorrectly.

The reason for this is that the controls try not to create unnecessary fonts; sometimes the font comes from the appearances and sometimes it just falls back to the ambient font.  It is important to know where the font came from so you will know whether you need to dispose it.

To determine the correct font and point size, you can do this:

Font fontToDispose;
try
{
    AppearanceData appData = new AppearanceData();
    AppearancePropFlags flags = AppearancePropFlags.FontData; 

    this.ultraLabel1.ResolveAppearance(ref appData, ref flags); 

    Font fontToDispose = appData.CreateFont(this.ultraLabel1.Font);
    Font font = fontToDispose != null ? fontToDispose : this.ultraLabel1.Font; 

    Debug.WriteLine(font.Name);
    Debug.WriteLine(font.SizeInPoints); 

    font = null;
}
finally
{
    if (fontToDispose != null)
        fontToDispose.Dispose();
}