Nikita
Nikita

Reputation: 308

Display text with HTML tags

I need to display HTML text on the page. Tryed to use WebView, but it can't fit HTML content height, it has only HeightRequest. I created renderer for Label to enable HTML tags in it.

public class HtmlLabelRenderer : LabelRenderer
{

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);   
        Control?.SetText(Html.FromHtml(Element.Text), TextView.BufferType.Spannable);
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == Label.TextProperty.PropertyName)
        {
            Control?.SetText(Html.FromHtml(Element.Text), TextView.BufferType.Spannable);
        }
    }
}

It works, but when my HTML text has tag with text-align:right, HtmlLabel doesn't show it. When I use WebView, tags with text-align:right displays correctly.

How can I update HtmlLabel to display tags with text-align:right correctly?

Upvotes: 1

Views: 1042

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24460

Not all HTML tags are supported by TextView.

According to a Commonsware's blog post the following tags are supported:

<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>

Although some inline css styles should work too. Make sure that your TextView is wide enough for the text to be displayed at the right edge.

Also according to this unit test from google, the text-align should be end rather than right. Which works for p, h1, h2, h3, h4, h5, h6, div, blockquote.

Upvotes: 2

Related Questions