Xamarin.Forms Formatted text
In this post I will write about Formatted text, this is very useful when you want to have different colors or styles in the same label. Actually Label have property FormattedText that allow to put text with different styles in the same. To do it, you need to add a List of Span, each Span has four properties, you can use all or only one, depends of you.
- Text
- Font
- ForegroundColor
- BackgroundColor
Now a small Example of it:
Code:
public class TestPage : ContentPage
{
public TestPage ()
{
FormattedString fs = new FormattedString ();
fs.Spans.Add (new Span(){Text="Hello "});
fs.Spans.Add (new Span(){Text="Xamarin Forms ", FontSize=12, FontAttributes=FontAttributes.Bold});
fs.Spans.Add (new Span(){Text="Is awesome!!",ForegroundColor=Color.Green});
Content = new StackLayout {
Children = {
new Label { FormattedText=fs,
HorizontalOptions=LayoutOptions.Center,
VerticalOptions=LayoutOptions.CenterAndExpand,
}
}
};
}
}
{
public TestPage ()
{
FormattedString fs = new FormattedString ();
fs.Spans.Add (new Span(){Text="Hello "});
fs.Spans.Add (new Span(){Text="Xamarin Forms ", FontSize=12, FontAttributes=FontAttributes.Bold});
fs.Spans.Add (new Span(){Text="Is awesome!!",ForegroundColor=Color.Green});
Content = new StackLayout {
Children = {
new Label { FormattedText=fs,
HorizontalOptions=LayoutOptions.Center,
VerticalOptions=LayoutOptions.CenterAndExpand,
}
}
};
}
}
And code working in iOS and Android:
More information in:
https://developer.xamarin.com/guides/cross-platform/xamarin-forms/
Follow me on Twitter
https://twitter.com/OsvaldoSan
Or find me in Linkedin
https://mx.linkedin.com/in/osvaldo-santiago-estrada-15480741
Comentarios
Publicar un comentario