Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit a0edab4

Browse files
samhoutsrmarinho
authored andcommitted
[iOS/Win] Label will not unnecessarily expand (#827)
* Add repro for 53362 * [iOS] Label will not unnecessarily expand * [Win] Label will not unnecessarily expand
1 parent 16f1423 commit a0edab4

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Xamarin.Forms.CustomAttributes;
2+
using Xamarin.Forms.Internals;
3+
4+
namespace Xamarin.Forms.Controls.Issues
5+
{
6+
[Preserve(AllMembers = true)]
7+
[Issue(IssueTracker.Bugzilla, 53362, "Layout regression in Grid on iOS: HorizontalOption = Center does not center", PlatformAffected.iOS)]
8+
public class Bugzilla53362 : TestContentPage
9+
{
10+
protected override void Init()
11+
{
12+
var label1 = new Label { Text = "auto sized row", TextColor = Color.Silver, HorizontalOptions = LayoutOptions.Center, BackgroundColor = Color.Purple };
13+
var label2 = new Label { Text = "row size 20", TextColor = Color.Silver, HorizontalOptions = LayoutOptions.Center, BackgroundColor = Color.Purple };
14+
var label3 = new Label { Text = "row size 25", TextColor = Color.Silver, HorizontalOptions = LayoutOptions.Center, BackgroundColor = Color.Purple };
15+
16+
var grid = new Grid
17+
{
18+
RowDefinitions =
19+
{
20+
new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) },
21+
new RowDefinition { Height = new GridLength(20, GridUnitType.Absolute) },
22+
new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) },
23+
new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) },
24+
}
25+
};
26+
27+
grid.Children.Add(label1, 0, 0);
28+
grid.Children.Add(label2, 0, 1);
29+
grid.Children.Add(label3, 0, 2);
30+
grid.Children.Add(new Label { Text = "If the three labels above are not all centered horizontally, this test has failed." }, 0, 3);
31+
32+
Content = grid;
33+
}
34+
}
35+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla51503.cs" />
236236
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla51505.cs" />
237237
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla52533.cs" />
238+
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla53362.cs" />
238239
<Compile Include="$(MSBuildThisFileDirectory)_Template.cs" />
239240
<Compile Include="$(MSBuildThisFileDirectory)Issue1028.cs" />
240241
<Compile Include="$(MSBuildThisFileDirectory)Issue1075.cs" />

Xamarin.Forms.Platform.WinRT/LabelRenderer.cs

+21-5
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,31 @@ public override SizeRequest GetDesiredSize(double widthConstraint, double height
7272
_perfectSizeValid = true;
7373
}
7474

75-
if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height)
75+
var widthFits = widthConstraint >= _perfectSize.Request.Width;
76+
var heightFits = heightConstraint >= _perfectSize.Request.Height;
77+
78+
if (widthFits && heightFits)
7679
return _perfectSize;
7780

7881
var result = base.GetDesiredSize(widthConstraint, heightConstraint);
79-
result.Minimum = new Size(Math.Min(10, result.Request.Width), result.Request.Height);
80-
if (Element.LineBreakMode != LineBreakMode.NoWrap)
82+
var tinyWidth = Math.Min(10, result.Request.Width);
83+
result.Minimum = new Size(tinyWidth, result.Request.Height);
84+
85+
if (widthFits || Element.LineBreakMode == LineBreakMode.NoWrap)
86+
return result;
87+
88+
bool containerIsNotInfinitelyWide = !double.IsInfinity(widthConstraint);
89+
90+
if (containerIsNotInfinitelyWide)
8191
{
82-
if (result.Request.Width > widthConstraint || Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap)
83-
result.Request = new Size(Math.Max(result.Minimum.Width, widthConstraint), result.Request.Height);
92+
bool textCouldHaveWrapped = Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap;
93+
bool textExceedsContainer = result.Request.Width > widthConstraint;
94+
95+
if (textExceedsContainer || textCouldHaveWrapped)
96+
{
97+
var expandedWidth = Math.Max(tinyWidth, widthConstraint);
98+
result.Request = new Size(expandedWidth, result.Request.Height);
99+
}
84100
}
85101

86102
return result;

Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs

+21-6
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,31 @@ public override SizeRequest GetDesiredSize(double widthConstraint, double height
2121
_perfectSizeValid = true;
2222
}
2323

24-
if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height)
24+
var widthFits = widthConstraint >= _perfectSize.Request.Width;
25+
var heightFits = heightConstraint >= _perfectSize.Request.Height;
26+
27+
if (widthFits && heightFits)
2528
return _perfectSize;
2629

2730
var result = base.GetDesiredSize(widthConstraint, heightConstraint);
28-
result.Minimum = new Size(Math.Min(10, result.Request.Width), result.Request.Height);
29-
if (Element.LineBreakMode != LineBreakMode.NoWrap)
31+
var tinyWidth = Math.Min(10, result.Request.Width);
32+
result.Minimum = new Size(tinyWidth, result.Request.Height);
33+
34+
if (widthFits || Element.LineBreakMode == LineBreakMode.NoWrap)
35+
return result;
36+
37+
bool containerIsNotInfinitelyWide = !double.IsInfinity(widthConstraint);
38+
39+
if (containerIsNotInfinitelyWide)
3040
{
31-
if (!double.IsInfinity(result.Request.Width) && !double.IsInfinity(widthConstraint))
32-
if (result.Request.Width > widthConstraint || Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap)
33-
result.Request = new Size(Math.Max(result.Minimum.Width, widthConstraint), result.Request.Height);
41+
bool textCouldHaveWrapped = Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap;
42+
bool textExceedsContainer = result.Request.Width > widthConstraint;
43+
44+
if (textExceedsContainer || textCouldHaveWrapped)
45+
{
46+
var expandedWidth = Math.Max(tinyWidth, widthConstraint);
47+
result.Request = new Size(expandedWidth, result.Request.Height);
48+
}
3449
}
3550

3651
return result;

0 commit comments

Comments
 (0)