WPF 中超链接中的上标/下标
我正在尝试创建一个包含带有上标和/或下标的文本的超链接。 我找到了两种方法来做到这一点,但它们都很糟糕。
解决方案#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>
(来源:excastle.com)< /p>
美丽的 Palatino Linotype; 但对于不支持变体的字体,它会被简单地忽略,不进行任何模拟,并且文本是全尺寸、基线、100% 正常。 我更愿意允许我的最终用户选择他们想要使用的字体,并且仍然可以使用超级/下标。
解决方案#2:使用 BaselineAlignment。 这会适当地升高或降低文本,但与解决方案 #1 不同,我必须手动减小字体大小。 尽管如此,它对所有字体都有效。 问题是超链接的下划线。
<TextBlock FontSize="30" FontFamily="Palatino Linotype">
<Hyperlink>
R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
</Hyperlink>
</TextBlock>
下划线随着文字一起升降,看起来相当猥琐。 我宁愿在整个超链接下有一个连续的、不间断的下划线。 (在有人建议边框之前,我还希望超链接能够自动换行,所有单词都带有下划线,包括第一行。)
有没有办法使上标和下标在 WPF 中工作,任何字体,下划线时不会看起来很糟糕?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果超链接不会换行超过一行,则可以嵌入另一个 TextBlock:
这将在超链接的子项下提供一个可靠的超链接,这意味着一个完整的超链接:
但是,如果嵌入的 TextBlock 需要换行为多行,则在整个换行段落下只会得到一个下划线,而不是为每一行文本添加下划线:
如果您可以仅在需要上标的一小部分内容周围放置 TextBlock(例如,仅在上例中的 R^2 周围),并将其余文本作为超链接的父级,那么您就会得到下划线像平常一样。 但有时这并不实用,因此需要注意。
If the hyperlink isn't going to wrap to more than one line, then embedding another TextBlock can work:
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:
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.
您可以使用上标 unicode 字符(例如 http://www.fileformat .info/info/unicode/char/b2/index.htm)
像这样:
结果:
显然,除非您的超级脚本实际上具有 unicode 上标字符,否则这将不起作用。
You can use the superscript unicode characters (e.g. http://www.fileformat.info/info/unicode/char/b2/index.htm)
Like this:
Result:
Obviously this will not work unless what you are super scripting actually has a unicode superscript character.