An Auto Scaling TextBlock for Silverlight

9 Mar
2010

During a recent Silverlight project which required extensive localisations into eight different European languages, I discovered just how much text length can vary between cultures. This caused a couple of issues where there was a limited area for a specific piece of text to fit within, which would look fine in English, but then would not fit in a different language and would be cut off.

I thought this would be a good idea for a custom control, and so have written an AutoScalingTextBlock control.

It’s a simple text control (based on ‘ContentControl’) that shrinks or expands the font of the text to display all of the text in the preferred size of the TextBlock. See it in action: http://edventuro.us/2010/03/sample-page-using-the-autoscalingtextblock/

While deciding how to get started, I came across an excellent TextTrimming TextBlock on Robby Ingebretsen’s site: http://blog.nerdplusart.com/archives/texttrimming-textblock-for-silverlight which I used as a basis, and so am releasing my control under the same MIT license that he used. I hope it comes in handy for someone.

AutoScalingTextBlock:

Dependency Properties:

  • MinFontSize: This is the smallest size the font will be reduced to. Defaults to 8pt.
  • MaxFontSize: This is the largest size the font will be increased to. Defaults to 20.
  • ScalingMode: This controls if the font size should be scaled only up, or down, or both ways to fit the text within the boundaries of the textbox.  Defaults to BothWays.
  • StepSize: This is the point size the font will be increased or decreased by each iteration until the text fits the desired size. Higher amounts will require fewer iterations, so will be faster, but the changes will be more abrupt. Defaults to 0.5.

Download the control: AutoScalingTextBlock.cs or a sample solution with examples in the Page.xaml file: AutoScalingControls.zip.  Free to use under the MIT licence.

kick it on DotNetKicks.com

3 Responses to An Auto Scaling TextBlock for Silverlight

Avatar

» Sample page using the AutoScalingTextBlock edventuro.us

March 9th, 2010 at 4:11 pm

[...] Here’s a sample page using the AutoScalingTextBlock referred to in the previous post (An Auto Scaling TextBlock for Silverlight). [...]

Avatar

Arash

August 26th, 2010 at 5:52 am

Wow, this works like gangbusters! Awesome awesome control

Avatar

Sasha

November 5th, 2010 at 9:29 pm

While I did appreciate the class provided, you can do the same without custom controls by simply using Viewbox control like this:

Hope this helps,
Sasha.

Comment Form

top