Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Fix Label Truncation on iOS with HorizontalOptions #14453

Merged
merged 13 commits into from
May 8, 2023
79 changes: 79 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;
using Xunit;

Expand Down Expand Up @@ -410,6 +411,84 @@ await InvokeOnMainThreadAsync(() =>
});
}

[Theory]
[InlineData(TextAlignment.Start, LineBreakMode.HeadTruncation)]
[InlineData(TextAlignment.Start, LineBreakMode.MiddleTruncation)]
[InlineData(TextAlignment.Start, LineBreakMode.TailTruncation)]
[InlineData(TextAlignment.Center, LineBreakMode.HeadTruncation)]
[InlineData(TextAlignment.Center, LineBreakMode.MiddleTruncation)]
[InlineData(TextAlignment.Center, LineBreakMode.TailTruncation)]
[InlineData(TextAlignment.End, LineBreakMode.HeadTruncation)]
[InlineData(TextAlignment.End, LineBreakMode.MiddleTruncation)]
[InlineData(TextAlignment.End, LineBreakMode.TailTruncation)]
public async Task LabelTruncatesCorrectly(TextAlignment textAlignment, LineBreakMode lineBreakMode)
{

EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<VerticalStackLayout, LayoutHandler>();
handlers.AddHandler<Label, LabelHandler>();
});
});

var labelStart = new Label
{
HorizontalOptions = LayoutOptions.Start,
LineBreakMode = lineBreakMode,
HorizontalTextAlignment = textAlignment,
Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
tj-devel709 marked this conversation as resolved.
Show resolved Hide resolved
};

var labelCenter = new Label
{
HorizontalOptions = LayoutOptions.Center,
LineBreakMode = lineBreakMode,
HorizontalTextAlignment = textAlignment,
Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
};

var labelEnd = new Label
{
HorizontalOptions = LayoutOptions.End,
LineBreakMode = lineBreakMode,
HorizontalTextAlignment = textAlignment,
Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
};

var labelFill = new Label
{
HorizontalOptions = LayoutOptions.Fill,
LineBreakMode = lineBreakMode,
HorizontalTextAlignment = textAlignment,
Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
};

var layout = new VerticalStackLayout()
{
labelStart,
labelCenter,
labelEnd,
labelFill,
};

layout.HeightRequest = 300;
layout.WidthRequest = 100;

await InvokeOnMainThreadAsync(async () =>
{
var contentViewHandler = CreateHandler<LayoutHandler>(layout);
await contentViewHandler.PlatformView.AttachAndRun(() =>
{
Assert.Equal(double.Round(labelStart.Width, 5), double.Round(layout.Width, 5));
hartez marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(double.Round(labelCenter.Width, 5), double.Round(layout.Width, 5));
Assert.Equal(double.Round(labelEnd.Width, 5), double.Round(layout.Width, 5));
Assert.Equal(double.Round(labelFill.Width, 5), double.Round(layout.Width, 5));
});
});
}

Color TextColor(LabelHandler handler)
{
#if __IOS__
Expand Down
10 changes: 9 additions & 1 deletion src/Core/src/Platform/iOS/MauiLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ public override void InvalidateIntrinsicContentSize()
}
}

public override SizeF SizeThatFits(SizeF size) => AddInsets(base.SizeThatFits(size));
public override SizeF SizeThatFits(SizeF size)
{
var requestedSize = base.SizeThatFits(size);

// Let's be sure the label is not larger than the container
tj-devel709 marked this conversation as resolved.
Show resolved Hide resolved
size.Width = nfloat.Min(requestedSize.Width, size.Width);
size.Height = requestedSize.Height;
return AddInsets(size);
}

SizeF AddInsets(SizeF size) => new SizeF(
width: size.Width + TextInsets.Left + TextInsets.Right,
Expand Down