在Windows应用程序中格式化标签内的文本

时间:2012-08-14 05:16:05

标签: .net wpf winforms

我想在我的标签上添加文字:area(m ^ 2)但是2应该是m的幂。

在.NET中可以这样做吗?

5 个答案:

答案 0 :(得分:3)

enter image description here

<Label Content="2 power of 3 => 3 &#x00B2; " />

而不是U+00B2我写了&#x00B2;

但是如果你想用C#代码编写它,你必须使用第一种格式

label.Content = "\u00B2";

答案 1 :(得分:1)

使用上标2字符²(unicode U + 00B2)。

答案 2 :(得分:1)

如果您需要上下文任意文本,可以设置TextBlock的Inlines属性:

<TextBlock>
    <TextBlock.Inlines>
        <Run Text="area (m"/>
        <Run Text="2" BaselineAlignment="Superscript"/>
        <Run Text=")"/>
    </TextBlock.Inlines>
</TextBlock>

您还可以减少上标文本的FontSize

如果你真的需要控件作为Label,你可以使用上面的TextBlock作为Label Content属性:

<Label>
    <TextBlock>
        <TextBlock.Inlines>
            <Run Text="area (m"/>
            <Run Text="2" BaselineAlignment="Superscript"/>
            <Run Text=")"/>
        </TextBlock.Inlines>
    </TextBlock>
</Label>

答案 3 :(得分:0)

这是解决您问题的WPF解决方案:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <FlowDocumentReader>
        <FlowDocument>
            <Paragraph>
                <Span>number</Span>
                <Span BaselineAlignment="Superscript">2</Span>
                <Span>number</Span>
                <Span BaselineAlignment="Subscript">indexed2</Span>
            </Paragraph>
        </FlowDocument>
    </FlowDocumentReader>
</Grid>

给出了下图中的结果:

enter image description here

答案 4 :(得分:-1)

您最好的选择是添加第二个标签以保持'2'并且位置高于标签1以使其感觉像是正方形。