From 05f0f7610f197ccc862e5a815376c3cd93d0efe8 Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Tue, 14 Mar 2017 03:51:28 -0700 Subject: [PATCH] [Win] Labels will now wrap when inside horizontally infinite layouts (#639) * Add repro for 42559 * [Win] Override GetDesiredSize for LabelRenderer * [Win] Invalidate size on font/align change --- .../Bugzilla42599.cs | 67 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Platform.WinRT/LabelRenderer.cs | 35 ++++++++++ 3 files changed, 103 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42599.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42599.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42599.cs new file mode 100644 index 00000000000..f10b3f4a3cc --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42599.cs @@ -0,0 +1,67 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +using System.Linq; +using System; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 42599, "LineBreakMode does not work on UWP", PlatformAffected.WinRT)] + public class Bugzilla42599 : TestContentPage + { + protected override void Init() + { + var scrollView = new ScrollView(); + var layout = new StackLayout(); + + foreach (var lineBreakMode in Enum.GetValues(typeof(LineBreakMode)).Cast()) + { + layout.Children.Add(GetLayout(lineBreakMode)); + } + scrollView.Content = layout; + Content = scrollView; + } + + static StackLayout GetLayout(LineBreakMode lineBreakMode) + { + var text = ""; + + switch (lineBreakMode) + { + default: + case LineBreakMode.NoWrap: + text = "This is a long sentence that should NOT wrap. If this sentence has wrapped, then this test has failed."; + break; + case LineBreakMode.WordWrap: + text = "This is a long sentence that should word wrap. If this sentence has NOT wrapped, then this test has failed."; + break; + case LineBreakMode.CharacterWrap: + text = "This is a long sentence that should character wrap. If this sentence has NOT wrapped, then this test has failed."; + break; + case LineBreakMode.HeadTruncation: + text = "This is a long sentence that should truncate at the beginning. If this sentence has NOT truncated, then this test has failed."; + break; + case LineBreakMode.TailTruncation: + text = "This is a long sentence that should truncate at the end. If this sentence has NOT truncated, then this test has failed."; + break; + case LineBreakMode.MiddleTruncation: + text = "This is a long sentence that should truncate at the middle. If this sentence has NOT truncated, then this test has failed."; + break; + } + + var label = new Label + { + LineBreakMode = lineBreakMode, + Text = text, + }; + + var layout = new StackLayout + { + Children = { label }, + Orientation = StackOrientation.Horizontal + }; + + return layout; + } + } +} \ No newline at end of file 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 38fc48a0c72..8ef92d46248 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 @@ -251,6 +251,7 @@ + diff --git a/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs b/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs index 33f1fee3a73..5101b167a8a 100644 --- a/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs @@ -35,6 +35,8 @@ public class LabelRenderer : ViewRenderer { bool _fontApplied; bool _isInitiallyDefault; + SizeRequest _perfectSize; + bool _perfectSizeValid; protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Size finalSize) { @@ -61,6 +63,29 @@ protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Si return finalSize; } + public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) + { + if (!_perfectSizeValid) + { + _perfectSize = base.GetDesiredSize(double.PositiveInfinity, double.PositiveInfinity); + _perfectSize.Minimum = new Size(Math.Min(10, _perfectSize.Request.Width), _perfectSize.Request.Height); + _perfectSizeValid = true; + } + + if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height) + return _perfectSize; + + var result = base.GetDesiredSize(widthConstraint, heightConstraint); + result.Minimum = new Size(Math.Min(10, result.Request.Width), result.Request.Height); + if (Element.LineBreakMode != LineBreakMode.NoWrap) + { + if (result.Request.Width > widthConstraint || Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap) + result.Request = new Size(Math.Max(result.Minimum.Width, widthConstraint), result.Request.Height); + } + + return result; + } + protected override void OnElementChanged(ElementChangedEventArgs