none
Auto font size for TextBox based on fixed height

    Question

  • I need to change font size, so that text will go inside TextBox.

    If i set Height property of TextBox to 15, font size must change, so that will go inside TextBox.
    I need that only Height is measurre, so i can't used ViewBox, since ViewBox also change TextBox width.

    Is there any code, that somebody written for that purpose?

    Friday, April 09, 2010 5:47 AM

All replies

  •  I have to change the fontsize every time the height is changed. I mean manually in code, in some event or the setter of height.  

    Friday, April 09, 2010 6:31 AM
  • Yes of course, but what is the code for do that.

    You probably don't use font size 100, and the decrease font size until font height is lower that control height.

    There must be something better to do.

    Saturday, April 10, 2010 8:45 AM
  • Create the TextBlock with a size say 20.  Immediately check the ActualHeight value of the text block, and you readjust the size accordingly.

    textBlock.FontSize = 20 * TargetHeight / textBlock.ActualHeight;

    I am pretty sure this works.  I have done some study of the relationship between font size and actual height, and it is linear.  In fact, my application Tagxedo http://www.tagxedo.com depends on such relationship.

    Saturday, April 10, 2010 12:49 PM
  •  What is: 20, TargetHeight.

    I need this for TextBox, not TextBlock.

    Sunday, April 18, 2010 4:30 AM
  • I also need to do this, has anyone discovered sample code anywhere ?

    Wednesday, May 26, 2010 2:13 AM
  •  Not yet, and i still need it.

    Saturday, May 29, 2010 3:54 AM
  • I use this method ( where 0.7 - prefered rate of text size, I got it by Font Size / Height of control ) and then used the same event handler on all my resizable text controls:

    // Event handler
    private void ControlsSizeChanged(object sender, System.Windows.SizeChangedEventArgs e) { GetFontSize(sender as Control); }


    // Method for font size changes
    public static void GetFontSize(Control control) { PropertyInfo info; if (control == null || control.ActualHeight <= 0) return; if(( info = control.GetType().GetProperty("FontSize", typeof(double))) != null) { info.SetValue(control, 0.7 * control.ActualHeight, null); } }




    Monday, May 30, 2011 12:20 PM