问题
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<sup><small>2</small></sup> + 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