Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set superscript and subscript in formatted text in wpf

How can I set some text as subscript/superscript in FormattedText in WPF?

like image 272
Firoz Avatar asked Jan 19 '10 17:01

Firoz


4 Answers

You use Typography.Variants:

<TextBlock>
    <Run>Normal Text</Run>
    <Run Typography.Variants="Superscript">Superscript Text</Run>
    <Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>
like image 50
Reed Copsey Avatar answered Nov 20 '22 15:11

Reed Copsey


You can use something like <TextBlock>5x<Run BaselineAlignment="Superscript">4</Run> + 4</TextBlock>.

However, as far as I know, you will have to reduce the font-size yourself.

like image 33
Matthias Avatar answered Nov 20 '22 14:11

Matthias


It's interesting to note that for some characters (m2, m3, etc) a superscript is not needed, but the unicode character can be used. For example:

<Run Text=" m&#x00B3;" />

This would show m3.

like image 18
Freek Sanders Avatar answered Nov 20 '22 15:11

Freek Sanders


I used a layout transform, because Typography.Variants often doesn't work:

<TextBlock Text="MyAmazingProduct"/>
 <TextBlock Text="TM">
  <TextBlock.LayoutTransform>
   <!-- Typography.Variants="Superscript" didn't work -->
   <TransformGroup>
    <ScaleTransform ScaleX=".75" ScaleY=".75"/>
    <TranslateTransform Y="-5"/>
   </TransformGroup>
  </TextBlock.LayoutTransform>
 </TextBlock>
<TextBlock Text="{Binding Path=Version, StringFormat={} v{0}}"/>

The advantage of using a LayoutTransform is that it is insensitive to the fontsize. If the fontsize is changed afterwards, this superscript works where explicit FontSize setting breaks.

like image 12
Ramon de Klein Avatar answered Nov 20 '22 13:11

Ramon de Klein