【问题标题】:Set superscript and subscript in formatted text in wpf在 wpf 中的格式化文本中设置上标和下标
【发布时间】:2011-01-06 22:11:21
【问题描述】:

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

【问题讨论】:

    标签: wpf wpf-controls subscript superscript formatted-text


    【解决方案1】:

    你使用Typography.Variants:

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

    【讨论】:

    • 至少在 .Net 4.0 中存在一些已知的错误:social.msdn.microsoft.com/Forums/en/wpf/thread/…。不知道 .Net 4.5 是否已修复。
    • 如果有人在 Win7 中遇到此错误,请查看此链接以修复它 support.microsoft.com/kb/2670838
    • 需要注意的是,Windows(和 WPF)的默认 UI 字体在 Windows 8 之前既不支持下标也不支持上标。
    【解决方案2】:

    你可以使用&lt;TextBlock&gt;5x&lt;Run BaselineAlignment="Superscript"&gt;4&lt;/Run&gt; + 4&lt;/TextBlock&gt;之类的东西。

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

    【讨论】:

    【解决方案3】:

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

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

    这将显示 m3

    【讨论】:

    【解决方案4】:

    我使用了布局转换,因为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 设置中断的地方。

    【讨论】:

    • Marvelous - 这是一个相当简洁且可配置的解决方案。谢谢!
    【解决方案5】:

    我不知道您是否需要将其用于 FormattedText 特别,或者您的意思是 Inline 的派生,但以下内容适用于 Inlines,即使 Typography.Variants="Superscript" 失败上班。

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

    希望对你有帮助!

    【讨论】:

    • 这在我的测试中惨遭失败,基于 RichTextBox 并且在粗体、斜体、下划线、字体系列/颜色/大小方面成功。我对所有这些都使用相同的 .ApplyPropertyValue() 。我使用ToggleButton,因此我验证了对齐是否已设置并被记住,但没有视觉效果。
    【解决方案6】:

    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>
    

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

        【解决方案8】:

        上标的设置适用于以下代码:

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

        在 Span 标签中为下标设置 Basealalignment 对我不起作用。 我尝试了以下代码,它运行良好。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-29
          • 1970-01-01
          • 2015-04-24
          • 2014-05-21
          • 2023-03-21
          • 2011-05-14
          • 2010-09-05
          • 1970-01-01
          相关资源
          最近更新 更多