0

I'm looking to add a feature to my app where the user can enter any string and select a background color, and it will create a square image with the string fitting perfectly inside.

I have found a few tutorials on how to add text to an image, but they all require you to set the font size. Is there any way the font can scale to the dimensions of the image based on the length of the string?

Here is an example of what I am trying to create.

Kilazur
  • 3,089
  • 1
  • 22
  • 48
Matt F
  • 204
  • 1
  • 11
  • You can measure the size of the rendered result (you don't really have to render it, look into `MeasureString`) and try several options until you find something that suits you well. – SimpleVar Sep 05 '14 at 14:23
  • [Have a look here](http://stackoverflow.com/questions/4152342/fill-text-inside-rectangle), I'm pretty sure it's related. – Kilazur Sep 05 '14 at 14:29

1 Answers1

0

Use a ViewBox to scale the text to the size of the container/image. Something like this:

<Grid Background="Blue">
    <Image Source="..." Width="200" Height="200" />
    <ViewBox Stretch="UniformToFill" StretchDirection="Both">
        <TextBlock Text="scaled to fit the Grid size" />
    </ViewBox>
</Grid>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260