PROWAREtech

articles » archived » xamarin » formatting-text-in-xaml

Xamarin: Formatting Text in XAML

How to format text in XAML.

To display a large amount of text like a paragraph, it is best to use the Text property attribute of the Label element as in examples one and two. To display text with line breaks it is easiest to use a <Label.Text> property element as in example three. If the text needs non-uniform formatting then use the <Label.FormattedText> property as in example four. To print curly brackets around your text, use a sequence of left and right curly braces first. These function as an escape sequence. See example 5.

<?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:local="clr-namespace:MobileAppXaml"
			 x:Class="MobileAppXaml.MainPage">

	<ContentPage.Padding>
		<OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0" />
	</ContentPage.Padding>

	<ScrollView>
		<StackLayout>

<!-- Example #1 -->
<Label x:Name="Ex1" VerticalOptions="CenterAndExpand" Text=
"This
is
simply
a
single
line
of
text
that
will
wrap
around
to
fit
the
screen
this
is
the
most
commonly
used" />

<!-- Example #2 -->
<Label x:Name="Ex2" VerticalOptions="CenterAndExpand" Text=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Donec egestas imperdiet purus, sit amet aliquet sapien
gravida quis. Curabitur sed velit eu est finibus malesuada
et eu nisl. Curabitur vel sapien et massa suscipit rhoncus.
Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas." />

<!-- Example #3 -->
<Label x:Name="Ex3" VerticalOptions="CenterAndExpand">
<Label.Text>
This
is
multiple
lines
of
text
each
word
occupies
one
line
</Label.Text>
</Label>

<!-- Example #4 -->
<Label x:Name="Ex4" VerticalOptions="CenterAndExpand">
	<Label.FormattedText>
		<FormattedString>
			<Span Text="A simple line of text " />
			<Span Text="that is italic" FontAttributes="Italic" />
			<Span Text=" or " />
			<Span Text="that is bold" FontAttributes="Bold" />
			<Span Text=" or " />
			<Span Text="that is large" FontSize="Large" />
		</FormattedString>
	</Label.FormattedText>
</Label>

<!-- Example #5 -->
<Label x:Name="Ex5" VerticalOptions="CenterAndExpand" Text="{}{This text will show}" />

		</StackLayout>
	</ScrollView>

</ContentPage>

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT