在wpf中设置格式化文本中的上标和下标

Fir*_*roz 38 wpf superscript wpf-controls subscript formatted-text

如何在wpf中的FormattedText中将一些文本设置为下标/上标

Ree*_*sey 46

您使用Typography.Variants:

<TextBlock>
    <Run>Normal Text</Run>
    <Run Typography.Variants="Superscript">Superscript Text</Run>
    <Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

  • 应该注意的是,Windows(和WPF)的默认UI字体既不支持Windows 8之前的下标也不支持上标. (6认同)
  • 至少从 .Net 4.0 开始,存在一些已知的错误:http://social.msdn.microsoft.com/Forums/en/wpf/thread/f375a41b-2c36-4e51-8f6b-7ed828431412。不知道.Net 4.5 中是否已修复。 (2认同)

Mat*_*ias 13

你可以使用类似的东西<TextBlock>5x<Run BaselineAlignment="Superscript">4</Run> + 4</TextBlock>.

但是,据我所知,你必须自己减少字体大小.


Ram*_*ein 10

我使用了布局转换,因为Typography.Variants通常不起作用:

<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}}"/>
Run Code Online (Sandbox Code Playgroud)

使用a的优点LayoutTransform是它对fontsize不敏感.如果之后更改了fontsize,则此上标适用于显式FontSize设置中断的位置.


Fre*_*ers 9

有趣的是,对于某些字符(m 2,m 3等),不需要上标,但可以使用unicode字符.例如:

<Run Text=" m&#x00B3;" />
Run Code Online (Sandbox Code Playgroud)

这将显示m 3.

  • 有关完整概述,请参阅维基百科:https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts (2认同)