Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 Autosize TextBlock to Maximum

Is there a way to set the font size of a TextBlock to the maximum allowed for the space available?

Here is a snippet of the code;

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock x:Name="myText" Text="Grow Me" TextAlignment="Center">
        </TextBlock>
    </Grid>
</Grid>

I'd like some think like FontSize=Auto|Maximum but they don't exist.

Thanks, Mike

EDIT: Here is the working code;

Thanks for pointing me in the right direction Allen.

while (myText.RenderSize.Width <= 450)
{
    myText.FontSize += 1;
    myText.UpdateLayout(); //Need this otherwise RenderSize doesn't change
}
like image 635
Mike Mengell Avatar asked Jan 21 '26 00:01

Mike Mengell


1 Answers

you can listen to the outter Grid's SizeChanged event.

Keep incrementing or decrementing your textblock font size until your TextBlock's rendered size is close to outter Grid's size.

Regards, Allen

like image 191
Haojie Avatar answered Jan 24 '26 03:01

Haojie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!