Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

xaml - Superscript / subscript in hyperlink in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...