有什么方法可以将HTML添加到Xamarin.Forms页面中? [英] Is there any way I can add HTML into a Xamarin.Forms page?

查看:477
本文介绍了有什么方法可以将HTML添加到Xamarin.Forms页面中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Xamarin.Forms页面中添加一些HTML?这可能吗?还可以将其添加为标签的一部分吗?

I would like to be able to add some HTML into a Xamarin.Forms page? Is this possible? Also is it possible to add this as part of a label?

推荐答案

如果您希望获得全面的html支持,那么Xamarin表单WebView将是可行的方法.但是,如果您想要Label中的某些基本格式支持,因为它是本机对应部分(Android中为TextView,iOS中为UILabel),则可以为此实现渲染器.

If you want full-blown html support, then Xamarin forms WebView would be way to go. But if you want some basic formatting support in Label as it's native counter-parts (TextView in android, and UILabel in iOS) do, you can implement renderers for that.

表单控件

public class HtmlLabel : Label
{
    public static readonly BindableProperty HtmlProperty =
        BindableProperty.Create(
            "Html", typeof(string), typeof(HtmlLabel),
            defaultValue: default(string));

    public string Html
    {
        get { return (string)GetValue(HtmlProperty); }
        set { SetValue(HtmlProperty, value); }
    }
}

iOS渲染器

[assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))]
namespace HtmlLabelApp.iOS
{
    public class HtmlLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            //if we have a new forms element, we want to update text with font style (as specified in forms-pcl) on native control
            if (e.NewElement != null)
                UpdateTextOnControl();
        }

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

            //if there is change in text or font-style, trigger update to redraw control
            if (e.PropertyName == nameof(HtmlLabel.Html))
            {
                UpdateTextOnControl();
            }
        }

        void UpdateTextOnControl()
        {
            if (Control == null)
                return;

            if (Element is HtmlLabel formsElement)
            {
                //set new text with ui-style-attributes to native control (UILabel)
                var htmlString = new NSString(formsElement.Html ?? string.Empty);
                var htmlData = htmlString.Encode(NSStringEncoding.UTF8);
                var attr = new NSAttributedStringDocumentAttributes
                {
                    DocumentType = NSDocumentType.HTML
                };

                var error = new NSError();
                var dict = new NSDictionary();

                var attributedString = new NSAttributedString(htmlData, attr, out dict, ref error);
                Control.AttributedText = attributedString;
            }
        }
    }
}

Android渲染器

[assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))]
namespace HtmlLabelApp.Droid
{
    public class HtmlLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            //if we have a new forms element, we want to update text with font style (as specified in forms-pcl) on native control
            if (e.NewElement != null)
                UpdateTextOnControl();
        }

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

            //if there is change in text or font-style, trigger update to redraw control
            if (e.PropertyName == nameof(HtmlLabel.Html))
            {
                UpdateTextOnControl();
            }
        }

        void UpdateTextOnControl()
        {
            if (Control == null)
                return;

            if (Element is HtmlLabel formsElement)
            {
                var htmlAsString = formsElement.Html ?? string.Empty;      // used by WebView
                var htmlAsSpanned = Html.FromHtml(htmlAsString, FromHtmlOptions.ModeCompact); // used by TextView

                Control.TextFormatted = htmlAsSpanned;
            }
        }
    }
}

样品用量

<local:HtmlLabel BackgroundColor="Silver">
    <local:HtmlLabel.Html>
        <x:String>
        <![CDATA[

            <h1>Main Title</h1>
            <h2>A sub-title</h2>
            <p>This is some html. Look, here\'s an <u>underline</u>.</p>
            <p>Look, this is <em>emphasized.</em> And here\'s some <b>bold</b>.</p>
            <p>This is a UL list:
            <ul>
             <li>One</li>
             <li>Two</li>
             <li>Three</li>
            </ul>
            <p>This is an OL list:
            <ol>
             <li>One</li>
             <li>Two</li>
             <li>Three</li>
            </ol>
        ]]>
        </x:String>
    </local:HtmlLabel.Html>
</local:HtmlLabel>

这篇关于有什么方法可以将HTML添加到Xamarin.Forms页面中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆