Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you justify text in Silverlight?

Does anyone have any suggestions on how to justify read-only text (rendered into a TextBlock) in Silverlight 2? WPF supports text justification by way of 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 gratefully received.

like image 213
PaulJ Avatar asked Nov 26 '22 12:11

PaulJ


1 Answers

Off the top of my head, I can think of two not-so-easy ways to do this. One is rather lame; adding spaces between the words. The other would be to somehow parse the text so that each word is it's own text block, you could then use a Grid to left justify the first word of a line and right justify the last word of a line, then space the other blocks in the center cell using a stack panel or similar.

Determining which words are the start and end of a line would involve measuring the rendered size of each block and deciding if it will fit. It's not simple but it should work.

like image 153
Jeff Yates Avatar answered Dec 16 '22 09:12

Jeff Yates