diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/LabelShouldSizeProperly.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/LabelShouldSizeProperly.png new file mode 100644 index 000000000000..e4fff0d06a6d Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/LabelShouldSizeProperly.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue29194.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue29194.cs new file mode 100644 index 000000000000..0c97f12434ee --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue29194.cs @@ -0,0 +1,37 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 29194, "[Android][Label] Setting a Label visible after having had focus on a InputView will increase the Label's height", PlatformAffected.Android)] +public class Issue29194 : ContentPage +{ + public Issue29194() + { + var mySwitch = new Switch() { AutomationId = "Switch" }; + + var label = new Label + { + Text = "Hello, World!", + BackgroundColor = Colors.Red, + AutomationId = "Label" + }; + + label.SetBinding(Label.IsVisibleProperty, new Binding(nameof(Switch.IsToggled), source: mySwitch)); + + var verticalStack = new VerticalStackLayout + { + Children = + { + label, + mySwitch, + new UITestEntry() {AutomationId = "Entry",IsCursorVisible=false}, + } + }; + + var scrollView = new ScrollView + { + Content = verticalStack + }; + + Content = scrollView; + + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/LabelShouldSizeProperly.png b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/LabelShouldSizeProperly.png new file mode 100644 index 000000000000..2b831a55debc Binary files /dev/null and b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/LabelShouldSizeProperly.png differ diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29194.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29194.cs new file mode 100644 index 000000000000..1bed535449e6 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29194.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; +public class Issue29194 : _IssuesUITest +{ + public Issue29194(TestDevice device) : base(device) { } + + public override string Issue => "[Android][Label] Setting a Label visible after having had focus on a InputView will increase the Label's height"; + + [Test] + [Category(UITestCategories.Label)] + public void LabelShouldSizeProperly() + { + App.WaitForElement("Entry"); + App.Tap("Entry"); + App.Tap("Switch"); + App.WaitForElement("Label"); + App.DismissKeyboard(); + VerifyScreenshot(); + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/LabelShouldSizeProperly.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/LabelShouldSizeProperly.png new file mode 100644 index 000000000000..00fa5e4a7ebc Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/LabelShouldSizeProperly.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/LabelShouldSizeProperly.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/LabelShouldSizeProperly.png new file mode 100644 index 000000000000..358ea5ccfb47 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/LabelShouldSizeProperly.png differ diff --git a/src/Core/src/Platform/Android/MauiTextView.cs b/src/Core/src/Platform/Android/MauiTextView.cs index 1f96c7926c1a..b5e3d82f858f 100644 --- a/src/Core/src/Platform/Android/MauiTextView.cs +++ b/src/Core/src/Platform/Android/MauiTextView.cs @@ -22,8 +22,19 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.AtMost && Layout is not null) { - int maxWidth = (int)Math.Ceiling(GetMaxLineWidth(Layout)) + CompoundPaddingLeft + CompoundPaddingRight; - widthMeasureSpec = MeasureSpec.MakeMeasureSpec(maxWidth, MeasureSpecMode.AtMost); + // Ensure the Layout is valid and measured before reading LineCount or GetLineWidth(i) to avoid unnecessary calculations. + if (Layout.Width > 0) + { + // Calculate the total width needed based on text content plus padding + int contentWidth = (int)Math.Ceiling(GetMaxLineWidth(Layout)); + int totalPadding = CompoundPaddingLeft + CompoundPaddingRight; + int requiredWidth = contentWidth + totalPadding; + + // Constrain to maximum available width from original spec + int availableWidth = MeasureSpec.GetSize(widthMeasureSpec); + int desiredWidth = Math.Min(requiredWidth, availableWidth); + widthMeasureSpec = MeasureSpec.MakeMeasureSpec(desiredWidth, MeasureSpecMode.AtMost); + } } base.OnMeasure(widthMeasureSpec, heightMeasureSpec); }