How would you justify the text in Silverlight?

Does anyone have any suggestions on how to justify read-only text (displayed in TextBlock ) in Silverlight 2? WPF supports text alignment through the TextAlignment enumeration:

 public enum TextAlignment { Left, Right, Center, Justify // <--- Missing from Silverlight :( } 

However, Silverlight 2 only supports the following:

 public enum TextAlignment { Center, Left, Right } 

Any ideas or suggestions are greatly appreciated.

+4
source share
5 answers

Above my head, I can think of two not-so-easy ways to do this. One is pretty lame; Add spaces between words. Another would be to somehow parse the text so that each word is its own text block, then you could use Grid to left to justify the first word of the line and right justify the last word of the line, and then a space in other blocks in the central cell with using a stack panel or similar.

Determining which words are the beginning and end of a line will include measuring the displayed size of each block and deciding whether it will fit. It is not easy, but it should work.

+1
source

I found a blog post about this - http://math-geek-rock-chick.blogspot.com/2008/08/silverlight-and-text-alignjustify.html . The idea is to split the text into separate lines, and then apply the ScaleTransform to each line, scaling the text on the left (RenderTransformOrigin = 0,0) to the right (Scale.X = 1.02). But the method is only good for small chunks of text and will probably work if your TextBox size changes.

0
source

Obviously, the only solution is to split the text string into words and layout words by column (as suggested by Jeff Yates above). The first thing to check using the Grid container:

 <Grid Name="grid1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock HorizontalAlignment="Left" Text="This" Grid.Column="0" /> <TextBlock HorizontalAlignment="Center" Text="is" Grid.Column="2" /> <TextBlock HorizontalAlignment="Center" Text="someprettylongpiece" Grid.Column="4" /> <TextBlock HorizontalAlignment="Right" Text="text" Grid.Column="6" /> </Grid> 

The next step is to create a custom panel that composes text blocks without dealing with the grid:

 <JustifiedPanel> <TextBlock Text="This"/> <TextBlock Text="is"/> <TextBlock Text="a"/> <TextBlock Text="justified"/> <TextBlock Text="line"/> <TextBlock Text="of"/> <TextBlock Text="text"/> <TextBlock Text="that"/> <TextBlock Text="demonstrates"/> <TextBlock Text="feasibility"/> </JustifiedPanel> 

(Sample source code is available on my blog ) Finally, I am going to create a JustifiedTextBlock control that will split the text into words and place them. The nontrivial things here are proper RTL support and proper line breaks.

0
source

Now you can use <RichTextBox> and it will be great.

If you want, this is a French ballet from Rudy Huyn: http://www.rudyhuyn.com/blog/2011/11/08/comment-justifier-du-texte-sous-windows-phone/

0
source

If they do not meet your needs, you should write an expander or converter and bind to it instead of using one of the enumerations.

-2
source

All Articles