Sometimes fractions are approximated using superscript and subscript text. That's not particularly accurate and can actually be a real pain to do in some cases. It's much better to let Silverlight handle that for you automatically.
However, there are certainly times when you want to have control over superscript and subscript text. Footnotes and chemistry are two places where superscript and subscript prevail.
Superscript and subscript are supported by the Typography.Variants attached property. Those aren't the only two variants supported, however. Some fonts, eastern fonts in particular, include support for several other variants including Inferior, Ordinal, and Ruby. Of course, we also have Normal for plain old text.
Figure 5 This shows the use of superscript and subscript in text using the Typography.Variants attached property.
Unlike fractions where the sizing was automatic, when using variants, you need to explicitly set the Typography.Variants property for each run of text you want affected. Listing 5 shows the markup required for this example.
Listing 5 Superscript and Subscript variants
<Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <StackPanel.Resources> <Style TargetType="TextBlock"> <Setter Property="FontFamily" Value="Gabriola" /> <Setter Property="FontSize" Value="75" /> <Setter Property="HorizontalAlignment" Value="Center" /> </Style> </StackPanel.Resources> <TextBlock> <Run Text="H" Typography.Variants="Normal" /> <Run Text="2" Typography.Variants="Subscript" /> #A <Run Text="O" Typography.Variants="Normal" /> </TextBlock> <TextBlock> <Run Text="i" Typography.Variants="Normal" /> <Run Text="2" Typography.Variants="Superscript" /> #B </TextBlock> <TextBlock> <Run Text="This is text with a pithy footnote" Typography.Variants="Normal" /> <Run Text="27" Typography.Variants="Superscript" /> </TextBlock> <TextBlock FontSize="40"> <Run Text="27 " Typography.Variants="Superscript" /> <Run Text="This is the pithy footnote" Typography.Variants="Normal" /> </TextBlock> </StackPanel></Grid> #A Subscript#B Superscript
As in the other examples, I used a style to keep the listing short. I did override the font size in the TextBlock which represents a footnote, however.
This example had a good bit more markup because, as mentioned, I had to set the Normal, Superscript, or Subscript variant for each block of text. Also note how these properties are available on the Run object inside a TextBlock as well. They're portable to just about anything using text.