Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show text with html format in xamarin forms

I work on webservice with json and i get text with html format. I want my text have hyperlinks and some other properties where i find from html tags (etc. bold).

I try binding my html string in WebView source but WebView is every time blank. I use this code

var browser = new WebView();
var htmlSource = new HTMLWebViewSource();
htmlSource.Html = MyItem.Article;
browser.Source = htmlSource;

MyItem.Article string is like this

json sample

I want something like this inside Label where is inside ListView os something like that.

text with hyperlinks

How can I do this?

like image 402
G.Mich Avatar asked Nov 02 '15 19:11

G.Mich


3 Answers

In XAML you can do something like this:

<WebView>
   <WebView.Source>
      <HtmlWebViewSource Html="{Binding HtmlText}"/>
   </WebView.Source>
</WebView>

You may also need to provide Height and Width of the WebView if it's not inside a Grid.

like image 93
Anup Sharma Avatar answered Oct 13 '22 14:10

Anup Sharma


FYI, I've just added the ability to my Forms9Patch library to create labels and buttons where you can format the text via HTML. For example:

new Forms9Patch.Label { HtmlText =  "plain <b><i>Bold+Italic</i></b> plain"}

... would give you a label where the text has been formatted bold italic in the middle of the string.

Also, as an aside, it allows you to use custom fonts that are embedded resources in your PCL project without any platform specific work. And, you can use these fonts via the HTLM <font> tag or and HTML font-family attribute.

Here are some screen shots from the demo app:

HtmlLabelDroid1 HtmlLabelApple3

like image 42
baskren Avatar answered Oct 13 '22 14:10

baskren


This should work for you

string htmlText = MyItem.Article.ToString().Replace(@"\", string.Empty);
var browser = new WebView ();
var html = new HtmlWebViewSource {
  Html = htmlText
};
browser.Source = html;

Because Xamarin.Forms.HtmlWebViewSource.HTML expect a pure HTML. Using this you can create a Xamarin.Forms user control with the help of this article http://blog.falafel.com/creating-reusable-xaml-user-controls-xamarin-forms/ Cheers..!

like image 45
Isham Mohamed Avatar answered Oct 13 '22 13:10

Isham Mohamed