locked
Stretch TextBlock to available bounds

    Question

  • Hello!

    I want to stretch TextBlock's text to stretch over full TextBlock's size (with automatic determination of maximum font size).

    Viewbox is not helpful at that because TextBlock inside it could not be multiline.

    In desktop app I use FormattedText class and iterate through different font sizes to determine maximum allowable size with which all the text fit TextBlock's bounds.

    But there is no such class in Windows Runtime API. Does another way of achieving this exist? Could I inherit the TextBlock and solve my problem in some way?

    Thank you.


    Vladimir Khil

    Wednesday, September 10, 2014 4:50 PM

Answers

  • Hi Vladimir,

    Per my understanding, you want to choose the suitable fontsize of a given string to fit the available textblock’s bounds. We can use Measure and arrange method in TextBlock class to get the ActualWidth and ActualHeight at runtime. ActualWidth and ActualHeight can help us to know the real width and height when rendering by the layout system.  Using this two methods, you can keep trying to find the right formatted string. See code snippet below.

     

    //create a new TextBlock at runtime
    
                TextBlock tbl= new TextBlock();
    
                tbl.Text = "I want to stretch TextBlock's text to stretch over full TextBlock's size (with automatic determination of maximum font size).";
    
                tbl.FontSize = 30;
    
                tbl.FontFamily = new FontFamily("Segoe UI Light");
    
                //before running measure and arrange method, actualwidth and actualheight will be 0
    
                double x=tbl.ActualHeight;
    
                double y=tbl.ActualWidth; 
    
                tbl.Measure(new Size(0, 0));
    
                tbl.Arrange(new Rect(0, 0, 0, 0));
    
                //after running the two method, you will find the actualwidth and actualheight. And then you can do what you want
    

    If I misunderstand you, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ur-Quan Friday, September 12, 2014 5:56 AM
    Friday, September 12, 2014 5:30 AM
    Moderator

All replies

  • Hi Vladimir,

    Per my understanding, you want to choose the suitable fontsize of a given string to fit the available textblock’s bounds. We can use Measure and arrange method in TextBlock class to get the ActualWidth and ActualHeight at runtime. ActualWidth and ActualHeight can help us to know the real width and height when rendering by the layout system.  Using this two methods, you can keep trying to find the right formatted string. See code snippet below.

     

    //create a new TextBlock at runtime
    
                TextBlock tbl= new TextBlock();
    
                tbl.Text = "I want to stretch TextBlock's text to stretch over full TextBlock's size (with automatic determination of maximum font size).";
    
                tbl.FontSize = 30;
    
                tbl.FontFamily = new FontFamily("Segoe UI Light");
    
                //before running measure and arrange method, actualwidth and actualheight will be 0
    
                double x=tbl.ActualHeight;
    
                double y=tbl.ActualWidth; 
    
                tbl.Measure(new Size(0, 0));
    
                tbl.Arrange(new Rect(0, 0, 0, 0));
    
                //after running the two method, you will find the actualwidth and actualheight. And then you can do what you want
    

    If I misunderstand you, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ur-Quan Friday, September 12, 2014 5:56 AM
    Friday, September 12, 2014 5:30 AM
    Moderator
  • Hi!

    Yes, you understand me correctly. Thank you for your advice. I only hope that Measure method work very fast to test a lot of possible font size values.

    Can I get some benefits from inheriting from TextBlock? Is there any method which I could override for faster solution? Maybe Measure method itself?


    Vladimir Khil


    • Edited by Ur-Quan Friday, September 12, 2014 5:56 AM
    Friday, September 12, 2014 5:56 AM