WPF中超链接中的上标/下标 [英] Superscript / subscript in hyperlink in WPF

查看:40
本文介绍了WPF中超链接中的上标/下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个包含带有上标和/或下标的文本的超链接.我找到了两种方法来做到这一点,但它们都很糟糕.

解决方案 #1:使用
(来源:

下划线随着文字上下起伏,看起来很猥琐.我宁愿在整个超链接下有一个连续的、不间断的下划线.(在有人建议边框之前,我还希望超链接能够自动换行,所有单词都带有下划线,包括第一行.)

有没有什么方法可以让上标和下标在 WPF 中以任何字体工作,而不会在下划线时看起来很糟糕?

解决方案

如果超链接不会换行超过一行,那么嵌入另一个 TextBlock 可以工作:

<超链接><文本块>R<Run BaselineAlignment=上标"FontSize=12pt">2</TextBlock></超链接></TextBlock>

这将在超链接的子项下给出一个实心超链接,这意味着一个完整的超链接:

<块引用>

但是,如果嵌入的 TextBlock 需要换行到多行,则整个换行段落下只会有一个下划线,而不是每一行文本下划线:

<块引用>


(来源:excastle.com)

如果您可以只在需要上标的一小段内容周围放置一个 TextBlock——例如,在上面的例子中只在 R^2 周围——并将其余的文本作为超链接的父级,那么你会得到正常下划线.但有时这不切实际,因此需要注意.

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.

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


(source: excastle.com)

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.

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.

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

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

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

解决方案

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

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

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:


(source: excastle.com)

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.

这篇关于WPF中超链接中的上标/下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆