Use this Xamarin.Forms plugin to display HTML content into a label.
** NEW YEAR UPDATE **
- Xamarin.Forms 2.5.0;
- .NET Standard 2.0;
- UWP partial support;
- Added support for: FontAttributes, FontFamily, FontSize, TextColor and HorizontalTextAlignment;
- HyperLinks support.
- Available on NuGet: https://www.nuget.org/packages/Xam.Plugin.HtmlLabel
- Install it in every Xamarin.Forms project.
- iOS: AppDelegate.cs
HtmlLabelRenderer.Initialize(); global::Xamarin.Forms.Forms.Init();
- Android: MainActivity.cs
HtmlLabelRenderer.Initialize(); global::Xamarin.Forms.Forms.Init(this, bundle);
- UWP: App.xaml.cs
var rendererAssemblies = new[] { typeof(HtmlLabelRenderer).GetTypeInfo().Assembly }; Xamarin.Forms.Forms.Init(e, rendererAssemblies); HtmlLabelRenderer.Initialize();
On iOS and Android it uses the native HTML rendering capabilities of iOS's UILabel and Android's TextView.
UWP's TextBlock cannot renders HTML so the library parses the HTML and uses Inlines to display: <a>, <b>, <br>, <em>, <i>, <p>, <strong>, <u>, <ul> <li>, <div>
.
FontAttributes, FontFamily, FontSize, TextColor, HorizontalTextAlignment are converted into inline CSS in a wrapping <div>
for iOS and Android. UWP supports them natively.
If you need to customize something in Android or iOS you can use inline CSS, for example:
<table style="border: 1px solid gray">...</table>
- Text
- FontAttributes
- FontFamily
- FontSize
- TextColor
- HorizontalTextAlignment
- MaxLines (int)
- IsHtml (bool)
- RemoveHtmlTags (bool)
xmlns:htmlLabel="clr-namespace:Plugin.HtmlLabel;assembly=Plugin.HtmlLabel"
<htmlLabel:HtmlLabel Text="{Binding HtmlString}"/>
xmlns:htmlLabel="clr-namespace:Plugin.HtmlLabel;assembly=Plugin.HtmlLabel"
<htmlLabel:HtmlLabel Text="{Binding HtmlString}" htmlLabel:HtmlLabel.MaxLines="2"/>
xmlns:htmlLabel="clr-namespace:Plugin.HtmlLabel;assembly=Plugin.HtmlLabel"
<htmlLabel:HtmlLabel Text="{Binding PlainTextString}" htmlLabel:HtmlLabel.IsHtml="False"/>
xmlns:htmlLabel="clr-namespace:Plugin.HtmlLabel;assembly=Plugin.HtmlLabel"
<htmlLabel:HtmlLabel Text="{Binding HtmlString}" htmlLabel:HtmlLabel.RemoveHtmlTags="True"/>
var label = new HtmlLabel();
label.Text = "..htmlstring.."
HtmlLabel.SetMaxLines(label, 3);
Contributions are welcome!
Under MIT, see LICENSE file.