WPF中超链接的上标/下标

时间:2021-08-01 20:11:34

I'm trying to make a Hyperlink that contains text with super- and/or subscripts. I've found two ways to do this, and both of them suck.

我正在尝试创建一个包含超级和/或下标文本的超链接。我找到了两种方法来做到这一点,而且两者都很糟糕。

Solution #1: use Typography.Variants. This gives a terrific superscript... for some fonts.

解决方案#1:使用Typography.Variants。对于某些字体,这给出了一个了不起的上标。

<StackPanel>
  <TextBlock FontFamily="Palatino Linotype" FontSize="30">
    <Hyperlink>R<Run Typography.Variants="Superscript">2</Run></Hyperlink>
    (Palatino Linotype)
  </TextBlock>
  <TextBlock FontFamily="Segoe UI" FontSize="30">
    <Hyperlink>R<Run Typography.Variants="Superscript">2</Run></Hyperlink>
    (Segoe UI)
  </TextBlock>
</StackPanel>

http://www.excastle.com/misc/wpf-superscript-fontvariants.png

http://www.excastle.com/misc/wpf-superscript-fontvariants.png

Looks beautiful in Palatino Linotype; but for fonts that don't support variants, it's simply ignored, no emulation is done, and the text is full-size, at-baseline, 100% normal. I would prefer to allow my end-users to select the font they want to use, and still have super/subscripts work.

在Palatino Linotype看起来很漂亮;但是对于不支持变体的字体,它只是被忽略,没有进行仿真,文本是全尺寸的,基线时,100%正常。我希望允许我的最终用户选择他们想要使用的字体,并且仍然有超级/下标工作。

Solution #2: use BaselineAlignment. This raises or lowers the text appropriately, though unlike solution #1, I have to decrease the font size manually. Still, it's effective for all fonts. The problem is the Hyperlink's underline.

解决方案#2:使用BaselineAlignment。这会适当地提高或降低文本,但与解决方案#1不同,我必须手动减小字体大小。不过,它对所有字体都有效。问题是Hyperlink的下划线。

<TextBlock FontSize="30" FontFamily="Palatino Linotype">
  <Hyperlink>
    R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
  </Hyperlink>
</TextBlock>

http://www.excastle.com/misc/wpf-superscript-baselinealignment.png

http://www.excastle.com/misc/wpf-superscript-baselinealignment.png

The underline is raised and lowered along with the text, which looks pretty wretched. I'd rather have a continuous, unbroken underline under the whole Hyperlink. (And before anyone suggests a Border, I'd also like the Hyperlink to be able to word-wrap, with all of the words underlined, including the first row.)

下划线与文本一起升高和降低,看起来非常可怜。我宁愿在整个超链接下有一个连续的,不间断的下划线。 (在任何人建议使用Border之前,我也希望Hyperlink能够自动换行,所有单词都加下划线,包括第一行。)

Is there any way to make superscript and subscript work in WPF, in any font, without looking laughably bad when underlined?

是否有任何方法可以在任何字体中使用WPF制作上标和下标,而在下划线时看起来不那么可笑?

2 个解决方案

#1


12  

If the hyperlink isn't going to wrap to more than one line, then embedding another TextBlock can work:

如果超链接不会包装到多行,则嵌入另一个TextBlock可以工作:

<TextBlock FontSize="30" FontFamily="Palatino Linotype">
  <Hyperlink>
    <TextBlock>
      R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
    </TextBlock>
  </Hyperlink>
</TextBlock>

This will give a solid hyperlink under the Hyperlink's child, which means an unbroken hyperlink:

这将在Hyperlink的子项下提供一个可靠的超链接,这意味着一个完整的超链接:

http://www.excastle.com/misc/wpf-superscript-baselinealignment-better.png

http://www.excastle.com/misc/wpf-superscript-baselinealignment-better.png

However, if the embedded TextBlock needs to wrap to multiple lines, you'll only get one underline under the entire wrapped paragraph, rather than underlining each line of text:

但是,如果嵌入的TextBlock需要换行到多行,那么在整个包装段落下只能得到一个下划线,而不是为每行文本加下划线:

http://www.excastle.com/misc/wpf-superscript-baselinealignment-badwrap.png

http://www.excastle.com/misc/wpf-superscript-baselinealignment-badwrap.png

If you can put a TextBlock only around a short bit of content that needs superscripts -- e.g., around just the R^2 in the above example -- and leave the rest of the text parented to the hyperlink, then you get underlining as normal. But sometimes that's not practical, so it's something to watch out for.

如果你只能在需要上标的一小部分内容周围放置一个TextBlock - 例如,在上面的例子中只围绕R ^ 2 - 并将剩下的文本保留为超链接的父级,那么你会得到正常的下划线。但有时这不实用,所以需要注意。

#2


1  

You can use the superscript unicode characters (e.g. http://www.fileformat.info/info/unicode/char/b2/index.htm)

您可以使用上标unicode字符(例如http://www.fileformat.info/info/unicode/char/b2/index.htm)

Like this:

喜欢这个:

<TextBlock FontSize="30" FontFamily="Segoe UI">
  <Hyperlink>
      Apply R² Calculation
  </Hyperlink>
</TextBlock>

Result:

结果:

WPF中超链接的上标/下标

Obviously this will not work unless what you are super scripting actually has a unicode superscript character.

显然这不起作用,除非你的超级脚本实际上有一个unicode上标字符。

#1


12  

If the hyperlink isn't going to wrap to more than one line, then embedding another TextBlock can work:

如果超链接不会包装到多行,则嵌入另一个TextBlock可以工作:

<TextBlock FontSize="30" FontFamily="Palatino Linotype">
  <Hyperlink>
    <TextBlock>
      R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
    </TextBlock>
  </Hyperlink>
</TextBlock>

This will give a solid hyperlink under the Hyperlink's child, which means an unbroken hyperlink:

这将在Hyperlink的子项下提供一个可靠的超链接,这意味着一个完整的超链接:

http://www.excastle.com/misc/wpf-superscript-baselinealignment-better.png

http://www.excastle.com/misc/wpf-superscript-baselinealignment-better.png

However, if the embedded TextBlock needs to wrap to multiple lines, you'll only get one underline under the entire wrapped paragraph, rather than underlining each line of text:

但是,如果嵌入的TextBlock需要换行到多行,那么在整个包装段落下只能得到一个下划线,而不是为每行文本加下划线:

http://www.excastle.com/misc/wpf-superscript-baselinealignment-badwrap.png

http://www.excastle.com/misc/wpf-superscript-baselinealignment-badwrap.png

If you can put a TextBlock only around a short bit of content that needs superscripts -- e.g., around just the R^2 in the above example -- and leave the rest of the text parented to the hyperlink, then you get underlining as normal. But sometimes that's not practical, so it's something to watch out for.

如果你只能在需要上标的一小部分内容周围放置一个TextBlock - 例如,在上面的例子中只围绕R ^ 2 - 并将剩下的文本保留为超链接的父级,那么你会得到正常的下划线。但有时这不实用,所以需要注意。

#2


1  

You can use the superscript unicode characters (e.g. http://www.fileformat.info/info/unicode/char/b2/index.htm)

您可以使用上标unicode字符(例如http://www.fileformat.info/info/unicode/char/b2/index.htm)

Like this:

喜欢这个:

<TextBlock FontSize="30" FontFamily="Segoe UI">
  <Hyperlink>
      Apply R² Calculation
  </Hyperlink>
</TextBlock>

Result:

结果:

WPF中超链接的上标/下标

Obviously this will not work unless what you are super scripting actually has a unicode superscript character.

显然这不起作用,除非你的超级脚本实际上有一个unicode上标字符。