Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue31782.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 31782, "Unexpected Line Breaks in Android, Label with WordWrap Mode Due to Trailing Space", PlatformAffected.Android)]
public class Issue31782 : ContentPage
{
public Issue31782()
{
var layout = new VerticalStackLayout { Spacing = 20, Padding = 20 };

var container = new Grid
{
AutomationId = "Container",
WidthRequest = 300,
HorizontalOptions = LayoutOptions.Center,
BackgroundColor = Colors.LightBlue
};

var label = new Label
{
AutomationId = "TestLabel",
Text = "How does .NET MAUI handle mauinavigaton?",
LineBreakMode = LineBreakMode.WordWrap,
TextColor = Color.FromArgb("#212121"),
FontSize = 20,
HorizontalOptions = LayoutOptions.End,
BackgroundColor = Colors.LightGreen
};

container.Add(label);
layout.Add(container);

Content = layout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue31782 : _IssuesUITest
{
public Issue31782(TestDevice device) : base(device) { }

public override string Issue => "Unexpected Line Breaks in Android, Label with WordWrap Mode Due to Trailing Space";

[Test]
[Category(UITestCategories.Label)]
public void LabelWithEndAlignmentShouldNotHaveTrailingSpace()
{
var labelRect = App.WaitForElement("TestLabel").GetRect();
var containerRect = App.WaitForElement("Container").GetRect();

// Before fix: Label width ≈ container width (300pt with trailing space)
// After fix: Label width < container width (measures only longest line)
Assert.That(labelRect.Width, Is.LessThan(containerRect.Width - 10),
$"End-aligned label ({labelRect.Width}pt) should be narrower than container ({containerRect.Width}pt)");
}
}
30 changes: 30 additions & 0 deletions src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Android.Text;
using Android.Views;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.Graphics;
Expand All @@ -15,6 +16,35 @@ public override void PlatformArrange(Rect frame)
base.PlatformArrange(frame);
}

public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
{
var size = base.GetDesiredSize(widthConstraint, heightConstraint);

// Android TextView reports full available width instead of actual text width when
// text wraps to multiple lines, causing incorrect positioning for non-Fill alignments.
if (VirtualView.HorizontalLayoutAlignment != Primitives.LayoutAlignment.Fill &&
PlatformView?.Layout is Layout layout &&
layout.LineCount > 1)
{
float maxLineWidth = 0;
for (int i = 0; i < layout.LineCount; i++)
{
float lineWidth = layout.GetLineWidth(i);
if (lineWidth > maxLineWidth)
maxLineWidth = lineWidth;
}

if (maxLineWidth > 0)
{
var actualWidth = Context.FromPixels(maxLineWidth + PlatformView.PaddingLeft + PlatformView.PaddingRight);
if (actualWidth < size.Width)
return new Size(actualWidth, size.Height);
}
}

return size;
}

internal static void MapBackground(ILabelHandler handler, ILabel label)
{
handler.PlatformView?.UpdateBackground(label);
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
override Microsoft.Maui.Handlers.LabelHandler.GetDesiredSize(double widthConstraint, double heightConstraint) -> Microsoft.Maui.Graphics.Size
Loading