Mike Mengell
Mike Mengell

Reputation: 2398

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
}

Upvotes: 3

Views: 1496

Answers (3)

Jean-Michel Bezeau
Jean-Michel Bezeau

Reputation: 11

Maybe the Viewbox was not available in May, but it is available now in November.

Thanks for the hint

Upvotes: 1

Matt Lacey
Matt Lacey

Reputation: 65586

The automatic way to do this is with a ViewBox but, unfortunately, this isn't available on the phone.

You'll have to resize the text yourself. :(

Upvotes: 0

Haojie
Haojie

Reputation: 5713

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

Upvotes: 1

Related Questions