zl程序教程

您现在的位置是:首页 >  其它

当前栏目

C# TextBlock 上标

2023-09-27 14:28:56 时间

我需要做一个函数,显示 <nobr><span class="math" id="MathJax-Span-37" style="width: 1.283em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.016em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.336em 1000em 2.509em -0.424em); top: -2.344em; left: 0.003em;"><span class="mrow" id="MathJax-Span-38"><span class="msubsup" id="MathJax-Span-39"><span style="display: inline-block; position: relative; width: 1.016em; height: 0px;"><span style="position: absolute; clip: rect(1.976em 1000em 2.723em -0.424em); top: -2.557em; left: 0.003em;"><span class="mi" id="MathJax-Span-40" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 2.563em;"></span></span><span style="position: absolute; top: -2.824em; left: 0.589em;"><span class="mn" id="MathJax-Span-41" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.456em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.349em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.203em; vertical-align: -0.063em;"></span></span></nobr><script type="math/tex" id="MathJax-Element-8"> x^2 </script> ,但是看起来用 TextBlock 做的不好看。

我用 WPF 写的上标看起来不好看,但是最后有了一个简单方法让他好看。

本文告诉大家如何做一个好看的上标。

<!--more-->

一开始做的方法:

把下面代码写在页面里,使用对齐是上面,改变字号,于是看起来就是上标。

          <TextBlock x:Name="TextBlock">
            <Run Text="y=x"></Run>
            <Run Text="2" BaselineAlignment="TextTop"
                 FontSize="8"></Run>
        </TextBlock>

于是看起来:

其实已经可以了,但是发现距离很大,那么如何让距离变小?

我找了很久,发现可以在 xaml.cs 上写。

            var textBlock = TextBlock;
            textBlock.Inlines.Add(new Run("y = "));
            textBlock.Inlines.Add(new Run("x"));
            Run run=new Run();
            run.FontSize = 7;
            run.BaselineAlignment = BaselineAlignment.TextTop;
            run.Text = "2";
            textBlock.Inlines.Add(run);

代码一样,但是写的地方不一样,可以看到现在的上标就好看了。

UWP 上标也一样。为什么写在 Xaml 间隔会那么大,是不是WR弄的?其实试试下面代码,注意不要格式化,直接写的样子和我的一样试试。

          <TextBlock x:Name="TextBlock">
            <TextBlock.Inlines>
                <Run Text="y=x"/><Run Text="2" BaselineAlignment="TextTop"
                 FontSize="8"/>
            </TextBlock.Inlines>
        </TextBlock>

原因就是Run写在两行,会把换行给记下,于是间隔就大了,写在一起的Run就不会出现这个距离。

但是我的 格式化会把Run放在下一行,所以可能我这里看的好的,在你这就会换行,看起来上标就有了距离。

我把他传上 csdn ,大家可以下载来验证。

代码:http://download.csdn.net/detail/lindexi_gd/9751879

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>