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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2095583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:25:45  来源:igfitidea点击:

Set superscript and subscript in formatted text in wpf

wpfwpf-controlssubscriptsuperscriptformatted-text

提问by Firoz

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

如何FormattedText在 WPF中将某些文本设置为下标/上标?

回答by Reed Copsey

You use Typography.Variants:

您使用Typography.Variants

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

回答by Matthias

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

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

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

但是,据我所知,您必须自己减小字体大小。

回答by Freek Sanders

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:

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

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

This would show m3.

这将显示 m 3

回答by Ramon de Klein

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

我使用了布局转换,因为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}}"/>

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

使用 a 的优点LayoutTransform是它对字体大小不敏感。如果随后更改了 fontsize,则此上标在显式 FontSize 设置中断的地方起作用。

回答by T. Webster

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.

我不知道您是否需要它专门用于 FormattedText ,或者您的意思是 Inline 的派生,但即使 Typography.Variants="Superscript" 无法工作,以下内容也适用于内联。

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

Hope it helps!

希望能帮助到你!

回答by Nitin Chaudhari

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:

Typography.Variants 仅适用于开放式字体。如果您不喜欢上标/下标超出实际文本的高度,那么您可以使用以下内容:

<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>

回答by MoMo

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>

回答by GCP

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.

在 Span 标签中为下标设置 Baseallignment 对我不起作用。我尝试了以下代码,效果很好。

  <TextBlock Text="H"  />
  <TextBlock Text="2" Margin="-2,0,-2,0" TextBlock.LineHeight="3" >   
  <TextBlock Text="O" />