在wpf中设置格式化文本的上标和下标
如何在 WPF 中的 FormattedText
中将某些文本设置为下标/上标?
How can I set some text as subscript/superscript in FormattedText
in WPF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您使用 Typography.Variants:
You use Typography.Variants:
有趣的是,对于某些字符(m2、m3 等)不需要上标,但可以使用 unicode 字符。例如:
这将显示 m3。
It's interesting to note that for some characters (m2, m3, etc) a superscript is not needed, but the unicode character can be used. For example:
This would show m3.
您可以使用类似
5x4 + 4
。但是,据我所知,您必须自己减小字体大小。
You can use something like
<TextBlock>5x<Run BaselineAlignment="Superscript">4</Run> + 4</TextBlock>
.However, as far as I know, you will have to reduce the font-size yourself.
我使用了布局转换,因为
Typography.Variants
通常不起作用:使用
LayoutTransform
的优点是它对字体大小不敏感。如果随后更改字体大小,则此上标将在显式 FontSize 设置中断的情况下起作用。I used a layout transform, because
Typography.Variants
often doesn't work:The advantage of using a
LayoutTransform
is that it is insensitive to the fontsize. If the fontsize is changed afterwards, this superscript works where explicit FontSize setting breaks.Typography.Variants 仅适用于开放类型字体。如果您不喜欢上标/下标超出实际文本的高度,那么您可以使用如下所示的内容:
Typography.Variants works only for open type fonts. If you dont like your superscripts/subscripts going outside the height of actual text then you can use something like the following:
我不知道您是否需要它与 FormattedText 一起使用,或者您的意思是 Inline 的派生,但以下内容将在内联上工作,即使 Typography.Variants="Superscript" 无法工作。
希望有帮助!
I don't know if you need this to work with FormattedText specifically, or you mean derivations of Inline, but the following will work on Inlines, even if Typography.Variants="Superscript" fails to work.
Hope it helps!
这是唯一对我有用的事情。它还使您可以更好地控制对齐方式和字体大小。
This is the only thing that worked for me. It also gives you more control over the alignment and font size.
设置上标可以很好地使用以下代码:
在 Span 标记中设置下标的 Basealignment 对我来说不起作用。
我尝试了以下代码,效果很好。
Setting for superscript works fine with the following code:
Setting the Baseallignment for subscript in the Span tag did not work for me.
I tried the following code and it worked fine.