簡介
演示如何在 Label 中顯示文本匾竿。
- 在 XAML 中創(chuàng)建 Xamarin.Forms Label别威。
- 更改 Label 的樣式。
- 在一個(gè) Label 中顯示具有多種格式的文本。
創(chuàng)建 Label
- 打開已有項(xiàng)目 AwesomeApp吁讨。
- 添加新項(xiàng) LabelPage.xaml。
- 編輯 LabelPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="AwesomeApp.LabelPage">
<ContentPage.Content>
<StackLayout>
<Label Text="歡迎使用 Xamarin.Forms峦朗!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
- 編輯 App.xaml.cs:
public App()
{
InitializeComponent();
MainPage = new LabelPage();
}
-
調(diào)試 Android 上的應(yīng)用:
配置 Label 樣式
- 編輯 LabelPage.xaml:
<Label Text="歡迎使用 Xamarin.Forms建丧!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
TextColor="Blue"
FontAttributes="Italic"
FontSize="22"
TextDecorations="Underline" />
TextColor 設(shè)置文本的顏色,F(xiàn)ontAttributes 設(shè)置字體為斜體波势,F(xiàn)ontSize 設(shè)置字號(hào)翎朱,TextDecorations 應(yīng)用下劃線效果
-
調(diào)試 Android 上的應(yīng)用:
自定義 Label 文本格式
- 編輯 LabelPage.xaml:
<Label TextColor="Gray"
FontSize="20"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<Label.FormattedText>
<FormattedString>
<Span Text="歡迎 "
TextColor="Blue"/>
<Span Text="使用 "
TextColor="Red"
FontAttributes="Italic" />
<Span Text="Xamarin.Form!"
TextColor="Green"
TextDecorations="Underline" />
</FormattedString>
</Label.FormattedText>
</Label>
FormattedText 屬性是 FormattedString 類型尺铣,可以包含一個(gè)或多個(gè) Span 實(shí)例
-
調(diào)試 Android 上的應(yīng)用: