Superscript text in Label

醉酒当歌 提交于 2021-02-16 20:08:08

问题


Is it possible to superscript a label in Xamarin Forms? In HTML I would use the <sup> tag. I want to superscript the text "min". So I can have the text "5min" with the "min" part superscripted.


回答1:


Thanks to Jason for his idea of using FormattedText property. But since it doesn't support binding of the text, I put 2 labels together in a StackLayout, the superscript text in mirco font size:

<StackLayout Orientation="Horizontal" Spacing="0">
                <Label Text="{Binding Minutes}">
                    <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                            <On Platform="Android" Value="fonts/chsans/MyFont-Bold.ttf#MyFont-Bold" />
                        </OnPlatform>
                    </Label.FontFamily>
                </Label>
                <Label Text="min" FontSize="Micro">
                    <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                            <On Platform="Android" Value="fonts/chsans/MyFont-Regular.ttf#MyFont-Regular" />
                        </OnPlatform>
                    </Label.FontFamily>
                </Label>
</StackLayout>



回答2:


Set the Label's TextType property to Html.

For example, to produce: x2 + y

In XAML:

<Label Text="x&lt;sup&gt;&lt;small&gt;2&lt;/small&gt;&lt;/sup&gt; + y" TextType="Html"/>

or

<Label TextType="Html">
    <![CDATA[
    x<sup><small>2</small></sup> + y
    ]]>
</Label>

In C#:

MyStackLayout.Children.Add(
    new Label {
        TextType=TextType.Html,
        Text="x<sup><small>2</small></sup> + y"
    });

Note that the <small> tag is optional.

For more information, see the "Display HTML" section of the Label documentation: Xamarin.Forms Label: Display HTML



来源:https://stackoverflow.com/questions/50475888/superscript-text-in-label

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!