Set superscript and subscript in formatted text in wpf
Asked Answered
H

8

45

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

Henry answered 19/1, 2010 at 17:37 Comment(0)
M
51

You use Typography.Variants:

<TextBlock>
    <Run>Normal Text</Run>
    <Run Typography.Variants="Superscript">Superscript Text</Run>
    <Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>
Mach answered 19/1, 2010 at 17:54 Comment(3)
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.Tennant
if some one get this bug with Win7 look at this link to fix it support.microsoft.com/kb/2670838Corrupt
It should be noted that the default UI font for Windows (and WPF) supports neither subscripts nor superscripts prior to Windows 8.Sostenuto
M
20

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.

Maggio answered 19/12, 2016 at 13:19 Comment(1)
See wikipedia for a complete overview: en.wikipedia.org/wiki/Unicode_subscripts_and_superscriptsSchweitzer
N
19

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.

Negotiation answered 19/1, 2010 at 17:43 Comment(3)
Is it normal that after each run a space is added? It works fine otherwise.Rapallo
Thanks, best solutionCranberry
the space is a known common problem #11090584Aleksandr
O
13

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.

Oilcup answered 7/8, 2014 at 14:23 Comment(1)
Marvelous - this is a rather neat and configurable solution. Thanks!Rotterdam
M
4

Typography.Variants works only for open type fonts. If you dont like your superscripts/subscripts going outside the height of actual text then you can use something like the following:

<StackPanel Orientation="Horizontal">
    <TextBlock FontSize="10" Margin="0,5,0,0">1</TextBlock>
    <TextBlock FontSize="30">H</TextBlock>
    <TextBlock FontSize="10" Margin="0,20,0,0">2</TextBlock>
</StackPanel>
Mireillemireles answered 1/11, 2010 at 13:12 Comment(0)
V
3

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!

Velda answered 3/10, 2010 at 23:32 Comment(1)
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.Cissy
I
2

This is the only thing that worked for me. It also gives you more control over the alignment and font size.

<TextBlock Grid.Row="17">
    3 x 3<Run FontSize="6pt" BaselineAlignment="TextTop">2</Run>)
</TextBlock>
Isolationist answered 13/3, 2015 at 15:55 Comment(0)
D
0

Setting for superscript works fine with the following code:

<TextBlock Text="(cm"  />
<TextBlock ><Span BaselineAlignment="Top" FontSize="8">2</Span></TextBlock>
<TextBlock Text=")" />

Setting the Baseallignment for subscript in the Span tag did not work for me. I tried the following code and it worked fine.

  <TextBlock Text="H"  />
  <TextBlock Text="2" Margin="-2,0,-2,0" TextBlock.LineHeight="3" >   
  <TextBlock Text="O" />
Deepfreeze answered 16/7, 2013 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.