Created
June 15, 2017 05:42
-
-
Save tormodfj/589d30b0f91e26f81cf5b82518396729 to your computer and use it in GitHub Desktop.
Xamarin Forms HTML-enabled Label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xamarin.Forms; | |
namespace MyProject.Controls | |
{ | |
public class HtmlLabel : Label | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using Android.OS; | |
using Android.Text; | |
using Android.Text.Method; | |
using Android.Widget; | |
using MyProject.Controls; | |
using MyProject.Droid.Renderers; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
[assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))] | |
namespace MyProject.Droid.Renderers | |
{ | |
public class HtmlLabelRenderer : LabelRenderer | |
{ | |
protected override void OnElementChanged(ElementChangedEventArgs<Label> e) | |
{ | |
base.OnElementChanged(e); | |
SetHtmlText(); | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged(sender, e); | |
if (e.PropertyName == Label.TextProperty.PropertyName) | |
{ | |
SetHtmlText(); | |
} | |
} | |
private void SetHtmlText() | |
{ | |
if(Control != null) | |
{ | |
Control.SetText(GetSpannedHtml(), TextView.BufferType.Spannable); | |
Control.MovementMethod = LinkMovementMethod.Instance; // Makes links clickable | |
} | |
} | |
private ISpanned GetSpannedHtml() | |
{ | |
var source = Element?.Text ?? ""; | |
var html = Build.VERSION.SdkInt >= BuildVersionCodes.N | |
? Html.FromHtml(source, FromHtmlOptions.ModeLegacy) | |
: Html.FromHtml(source); | |
return html; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using CoreGraphics; | |
using Foundation; | |
using MyProject.Controls; | |
using MyProject.iOS.Renderers; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
[assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))] | |
namespace MyProject.iOS.Renderers | |
{ | |
public class HtmlLabelRenderer : ViewRenderer<HtmlLabel, UITextView> | |
{ | |
public string FontStyle => "<style>body{font-family:Helvetica;font-size:12pt;}</style>"; | |
protected override void OnElementChanged(ElementChangedEventArgs<HtmlLabel> e) | |
{ | |
base.OnElementChanged(e); | |
if (Element == null) | |
return; | |
var textView = new UITextView(new CGRect(0, 0, Element.Width, Element.Height)) | |
{ | |
Font = UIFont.SystemFontOfSize((float)Element.FontSize), | |
Editable = false, | |
Bounces = false, | |
DataDetectorTypes = UIDataDetectorType.All, // Makes links clickable | |
BackgroundColor = UIColor.Clear | |
}; | |
SetNativeControl(textView); | |
SetHtmlText(); | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged(sender, e); | |
if (e.PropertyName == Label.TextProperty.PropertyName) | |
{ | |
SetHtmlText(); | |
} | |
} | |
private void SetHtmlText() | |
{ | |
var source = FontStyle + (Element?.Text ?? ""); | |
var attr = new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.HTML }; | |
var nsError = new NSError(); | |
var html = NSData.FromString(source, NSStringEncoding.Unicode); | |
Control.AttributedText = new NSAttributedString(html, attr, ref nsError); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment