diff options
author | Samantha Houts <samantha@teamredwall.com> | 2017-03-22 03:43:17 -0700 |
---|---|---|
committer | Rui Marinho <me@ruimarinho.net> | 2017-03-22 10:43:17 +0000 |
commit | c65a9a8c57d93b0eff38fd8b06223f6a04688c59 (patch) | |
tree | 1392a70d29802674f814c3222dd422451cd7ea47 | |
parent | 7538350f6d750f5e29d011834cf36a17a1e045e3 (diff) | |
download | xamarin-forms-c65a9a8c57d93b0eff38fd8b06223f6a04688c59.tar.gz xamarin-forms-c65a9a8c57d93b0eff38fd8b06223f6a04688c59.tar.bz2 xamarin-forms-c65a9a8c57d93b0eff38fd8b06223f6a04688c59.zip |
[iOS/Win] Label will not unnecessarily expand (#827)
* Add repro for 53362
* [iOS] Label will not unnecessarily expand
* [Win] Label will not unnecessarily expand
4 files changed, 78 insertions, 11 deletions
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla53362.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla53362.cs new file mode 100644 index 00000000..1073c0f3 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla53362.cs @@ -0,0 +1,35 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 53362, "Layout regression in Grid on iOS: HorizontalOption = Center does not center", PlatformAffected.iOS)] + public class Bugzilla53362 : TestContentPage + { + protected override void Init() + { + var label1 = new Label { Text = "auto sized row", TextColor = Color.Silver, HorizontalOptions = LayoutOptions.Center, BackgroundColor = Color.Purple }; + var label2 = new Label { Text = "row size 20", TextColor = Color.Silver, HorizontalOptions = LayoutOptions.Center, BackgroundColor = Color.Purple }; + var label3 = new Label { Text = "row size 25", TextColor = Color.Silver, HorizontalOptions = LayoutOptions.Center, BackgroundColor = Color.Purple }; + + var grid = new Grid + { + RowDefinitions = + { + new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }, + new RowDefinition { Height = new GridLength(20, GridUnitType.Absolute) }, + new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) }, + new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }, + } + }; + + grid.Children.Add(label1, 0, 0); + grid.Children.Add(label2, 0, 1); + grid.Children.Add(label3, 0, 2); + grid.Children.Add(new Label { Text = "If the three labels above are not all centered horizontally, this test has failed." }, 0, 3); + + Content = grid; + } + } +}
\ 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 1b060efa..326d1323 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 @@ -261,6 +261,7 @@ <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51503.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51505.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Bugzilla52533.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)Bugzilla53362.cs" /> <Compile Include="$(MSBuildThisFileDirectory)_Template.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue1028.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue1075.cs" /> diff --git a/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs b/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs index 5101b167..1a4b0931 100644 --- a/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT/LabelRenderer.cs @@ -72,15 +72,31 @@ namespace Xamarin.Forms.Platform.WinRT _perfectSizeValid = true; } - if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height) + var widthFits = widthConstraint >= _perfectSize.Request.Width; + var heightFits = heightConstraint >= _perfectSize.Request.Height; + + if (widthFits && heightFits) 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) + var tinyWidth = Math.Min(10, result.Request.Width); + result.Minimum = new Size(tinyWidth, result.Request.Height); + + if (widthFits || Element.LineBreakMode == LineBreakMode.NoWrap) + return result; + + bool containerIsNotInfinitelyWide = !double.IsInfinity(widthConstraint); + + if (containerIsNotInfinitelyWide) { - 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); + bool textCouldHaveWrapped = Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap; + bool textExceedsContainer = result.Request.Width > widthConstraint; + + if (textExceedsContainer || textCouldHaveWrapped) + { + var expandedWidth = Math.Max(tinyWidth, widthConstraint); + result.Request = new Size(expandedWidth, result.Request.Height); + } } return result; diff --git a/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs index b8b81e99..82ceda81 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs @@ -30,16 +30,31 @@ namespace Xamarin.Forms.Platform.MacOS _perfectSizeValid = true; } - if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height) + var widthFits = widthConstraint >= _perfectSize.Request.Width; + var heightFits = heightConstraint >= _perfectSize.Request.Height; + + if (widthFits && heightFits) 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) + var tinyWidth = Math.Min(10, result.Request.Width); + result.Minimum = new Size(tinyWidth, result.Request.Height); + + if (widthFits || Element.LineBreakMode == LineBreakMode.NoWrap) + return result; + + bool containerIsNotInfinitelyWide = !double.IsInfinity(widthConstraint); + + if (containerIsNotInfinitelyWide) { - if (!double.IsInfinity(result.Request.Width) && !double.IsInfinity(widthConstraint)) - 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); + bool textCouldHaveWrapped = Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap; + bool textExceedsContainer = result.Request.Width > widthConstraint; + + if (textExceedsContainer || textCouldHaveWrapped) + { + var expandedWidth = Math.Max(tinyWidth, widthConstraint); + result.Request = new Size(expandedWidth, result.Request.Height); + } } return result; |