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

发布于 2024-08-18 05:46:56 字数 60 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

归属感 2024-08-25 05:46:56

您使用 Typography.Variants

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

You use Typography.Variants:

<TextBlock>
    <Run>Normal Text</Run>
    <Run Typography.Variants="Superscript">Superscript Text</Run>
    <Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>
勿忘心安 2024-08-25 05:46:56

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

<Run Text=" m³" />

这将显示 m3

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.

我不会写诗 2024-08-25 05:46:56

您可以使用类似 5x4+ 4

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

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.

妄断弥空 2024-08-25 05:46:56

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

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

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.

疯了 2024-08-25 05:46:56

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>

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>
停滞 2024-08-25 05:46:56

我不知道您是否需要它与 FormattedText 一起使用,或者您的意思是 Inline 的派生,但以下内容将在内联上工作,即使 Typography.Variants="Superscript" 无法工作。

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

希望有帮助!

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!

烈酒灼喉 2024-08-25 05:46:56

这是唯一对我有用的事情。它还使您可以更好地控制对齐方式和字体大小。

<TextBlock Grid.Row="17">
    3 x 3<Run FontSize="6pt" BaselineAlignment="TextTop">2</Run>)
</TextBlock>

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>
待天淡蓝洁白时 2024-08-25 05:46:56

设置上标可以很好地使用以下代码:

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

在 Span 标记中设置下标的 Basealignment 对我来说不起作用。
我尝试了以下代码,效果很好。

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

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文