Set superscript and subscript in formatted text in wpf

29,158

Solution 1

You use Typography.Variants:

<TextBlock>
    <Run>Normal Text</Run>
    <Run Typography.Variants="Superscript">Superscript Text</Run>
    <Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

I don't know if you need this to work with FormattedText specifically, or you mean derivations of Inline, but the following will work on Inlines, even if Typography.Variants="Superscript" fails to work.

TextRange selection = new TextRange(document.ContentStart, document.ContentEnd);
selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Superscript);

Hope it helps!

Share:
29,158

Related videos on Youtube

Firoz
Author by

Firoz

Updated on May 05, 2020

Comments

  • Firoz
    Firoz about 4 years

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

  • skybluecodeflier
    skybluecodeflier over 12 years
    There are some known bugs with this, at least as of .Net 4.0: social.msdn.microsoft.com/Forums/en/wpf/thread/…. Don't know if it is fixed in .Net 4.5.
  • WiiMaxx
    WiiMaxx over 11 years
    if some one get this bug with Win7 look at this link to fix it support.microsoft.com/kb/2670838
  • Chris Kerekes
    Chris Kerekes about 11 years
    It should be noted that the default UI font for Windows (and WPF) supports neither subscripts nor superscripts prior to Windows 8.
  • David Brunelle
    David Brunelle almost 10 years
    Is it normal that after each run a space is added? It works fine otherwise.
  • person27
    person27 over 6 years
    This fails miserably in my tests, based on RichTextBox and otherwise successful with bold, italic, underline, font family/color/size. I use the same .ApplyPropertyValue() with all of those. I useToggleButton's so I verify the alignment is set and remembered, but without visual effect.
  • Jeb
    Jeb about 6 years
    Marvelous - this is a rather neat and configurable solution. Thanks!
  • Ugur
    Ugur almost 5 years
    Thanks, best solution
  • Wouter
    Wouter about 4 years
    See wikipedia for a complete overview: en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
  • Eric
    Eric over 2 years
    the space is a known common problem stackoverflow.com/questions/11090084/…