From 938a840e68bc6f1f26974ee8be0eb7a4ebcbeda2 Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Wed, 28 Aug 2019 13:25:02 +0200 Subject: [PATCH] Make Label display HTML from a string (#4527) * Use UpdateText * Added missing helper method and UI test * Added missing helper for UWP * Added csproj entry for helper * Resolved rebase conflicts * Update LabelRenderer.cs * Update LabelRenderer.cs * Update LabelRenderer.cs * iOS Merge error fix * Feedback * - uwp fixes * - android fix empty text * - ios fix null and setting text when texttype starts as html * - set _perfectSizeValid = false; after changed AttributedText Setting the AttributedText causes GetDesiredSize to get called which sets _perfectSizeValid to true but at this point this frame still hasn't adjusted to any size change from *LayoutSubViews*. This resets _perfectSizeValid so after the AttributedText set the desiredsize can get pulled again * Renamed PlainText to Text * Fixed initial no HTML styling --- .../LabelTextType.cs | 88 ++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + .../CoreGalleryPages/LabelCoreGalleryPage.cs | 37 +++++++ Xamarin.Forms.Core/Label.cs | 9 ++ Xamarin.Forms.Core/TextType.cs | 8 ++ Xamarin.Forms.CustomAttributes/TestAttributes.cs | 3 +- .../FastRenderers/LabelRenderer.cs | 24 ++++- Xamarin.Forms.Platform.Android/Forms.cs | 11 ++ .../Renderers/LabelRenderer.cs | 26 ++++- Xamarin.Forms.Platform.UAP/LabelHtmlHelper.cs | 112 +++++++++++++++++++++ Xamarin.Forms.Platform.UAP/LabelRenderer.cs | 46 +++++++-- .../Xamarin.Forms.Platform.UAP.csproj | 1 + .../Renderers/LabelRenderer.cs | 66 +++++++++++- 13 files changed, 414 insertions(+), 18 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/LabelTextType.cs create mode 100644 Xamarin.Forms.Core/TextType.cs create mode 100644 Xamarin.Forms.Platform.UAP/LabelHtmlHelper.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/LabelTextType.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/LabelTextType.cs new file mode 100644 index 0000000..3590c08 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/LabelTextType.cs @@ -0,0 +1,88 @@ +using System; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.Forms.Core.UITests; +using Xamarin.UITest; +using NUnit.Framework; +using System.Linq; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ +#if UITEST + [Category(UITestCategories.Label)] +#endif + [Preserve(AllMembers = true)] + [Issue(IssueTracker.None, 0, "Implementation of Label TextType", PlatformAffected.All)] + public class LabelTextType : TestContentPage + { + protected override void Init() + { + var label = new Label + { + AutomationId = "TextTypeLabel", + Text = "

Hello World!

" + }; + + var button = new Button + { + AutomationId = "ToggleTextTypeButton", + Text = "Toggle HTML/Plain" + }; + + button.Clicked += (s, a) => + { + label.TextType = label.TextType == TextType.Html ? TextType.Text : TextType.Html; + }; + + + Label htmlLabel = new Label() { TextType = TextType.Html }; + Label normalLabel = new Label(); + Label nullLabel = new Label() { TextType = TextType.Html }; + + Button toggle = new Button() + { + Text = "Toggle some more things", + Command = new Command(() => + { + htmlLabel.Text = $"{DateTime.UtcNow}"; + normalLabel.Text = $"{DateTime.UtcNow}"; + + if (String.IsNullOrWhiteSpace(nullLabel.Text)) + nullLabel.Text = "hi there"; + else + nullLabel.Text = null; + }) + }; + + + var stacklayout = new StackLayout(); + stacklayout.Children.Add(label); + stacklayout.Children.Add(button); + stacklayout.Children.Add(htmlLabel); + stacklayout.Children.Add(normalLabel); + stacklayout.Children.Add(nullLabel); + stacklayout.Children.Add(toggle); + + Content = stacklayout; + } + +#if UITEST + [Test] + public void LabelToggleHtmlAndPlainTextTest() + { + RunningApp.WaitForElement ("TextTypeLabel"); + RunningApp.Screenshot ("I see plain text"); + + Assert.IsTrue(RunningApp.Query("TextTypeLabel").FirstOrDefault()?.Text == "

Hello World!

"); + + RunningApp.Tap("ToggleTextTypeButton"); + RunningApp.Screenshot ("I see HTML text"); + + Assert.IsFalse(RunningApp.Query("TextTypeLabel").FirstOrDefault()?.Text.Contains("

") ?? true); + } +#endif + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 1711c8f..2fb5d14 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -1029,6 +1029,7 @@ + diff --git a/Xamarin.Forms.Controls/CoreGalleryPages/LabelCoreGalleryPage.cs b/Xamarin.Forms.Controls/CoreGalleryPages/LabelCoreGalleryPage.cs index e7d0c67..9d7bbeb 100644 --- a/Xamarin.Forms.Controls/CoreGalleryPages/LabelCoreGalleryPage.cs +++ b/Xamarin.Forms.Controls/CoreGalleryPages/LabelCoreGalleryPage.cs @@ -236,6 +236,40 @@ namespace Xamarin.Forms.Controls Padding = new Thickness(40, 20) } ); + + var htmlLabelContainer = new ViewContainer