Set superscript and subscript in formatted text in wpf
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³" />
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!
Related videos on Youtube
Firoz
Updated on May 05, 2020Comments
-
Firoz about 4 years
How can I set some text as subscript/superscript in
FormattedText
in WPF? -
skybluecodeflier over 12 yearsThere 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 over 11 yearsif some one get this bug with Win7 look at this link to fix it support.microsoft.com/kb/2670838
-
Chris Kerekes about 11 yearsIt should be noted that the default UI font for Windows (and WPF) supports neither subscripts nor superscripts prior to Windows 8.
-
David Brunelle almost 10 yearsIs it normal that after each run a space is added? It works fine otherwise.
-
person27 over 6 yearsThis 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 about 6 yearsMarvelous - this is a rather neat and configurable solution. Thanks!
-
Ugur almost 5 yearsThanks, best solution
-
Wouter about 4 yearsSee wikipedia for a complete overview: en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
-
Eric over 2 yearsthe space is a known common problem stackoverflow.com/questions/11090084/…