diff --git a/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs b/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs index 3c7ddc6eebd0..ceb267150b7a 100644 --- a/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs @@ -458,6 +458,100 @@ 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)] + [InlineData(TextAlignment.Start, LineBreakMode.NoWrap)] + [InlineData(TextAlignment.Center, LineBreakMode.NoWrap)] + [InlineData(TextAlignment.End, LineBreakMode.NoWrap)] + public async Task LabelTruncatesCorrectly(TextAlignment textAlignment, LineBreakMode lineBreakMode) + { + EnsureHandlerCreated(builder => + { + builder.ConfigureMauiHandlers(handlers => + { + handlers.AddHandler(); + handlers.AddHandler(); + }); + }); + + var labelStart = new Label + { + HorizontalOptions = LayoutOptions.Start, + LineBreakMode = lineBreakMode, + HorizontalTextAlignment = textAlignment, + Text = LoremIpsum, + }; + + var labelCenter = new Label + { + HorizontalOptions = LayoutOptions.Center, + LineBreakMode = lineBreakMode, + HorizontalTextAlignment = textAlignment, + Text = LoremIpsum, + }; + + var labelEnd = new Label + { + HorizontalOptions = LayoutOptions.End, + LineBreakMode = lineBreakMode, + HorizontalTextAlignment = textAlignment, + Text = LoremIpsum, + }; + + var labelFill = new Label + { + HorizontalOptions = LayoutOptions.Fill, + LineBreakMode = lineBreakMode, + HorizontalTextAlignment = textAlignment, + Text = LoremIpsum, + }; + + var layout = new VerticalStackLayout() + { + labelStart, + labelCenter, + labelEnd, + labelFill, + }; + + layout.HeightRequest = 300; + layout.WidthRequest = 100; + +#if WINDOWS + await AttachAndRun(layout, (handler) => + { + var layoutPlatWidth = handler.PlatformView.Width; + Assert.Equal(labelStart.Width, layoutPlatWidth); + Assert.Equal(labelCenter.Width, layoutPlatWidth); + Assert.Equal(labelEnd.Width, layoutPlatWidth); + Assert.Equal(double.Round(labelFill.Width, 5), double.Round(layoutPlatWidth, 5)); + }); + +#else + await InvokeOnMainThreadAsync(async () => + { + var contentViewHandler = CreateHandler(layout); + await contentViewHandler.PlatformView.AttachAndRun(() => + { + Assert.Equal(double.Round(labelStart.Width, 5), double.Round(layout.Width, 5)); + 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)); + }); + }); +#endif + } + + static readonly string LoremIpsum = "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."; + [Fact] public async Task TextTypeAfterFontStuffIsCorrect() { diff --git a/src/Core/src/Platform/iOS/MauiLabel.cs b/src/Core/src/Platform/iOS/MauiLabel.cs index 4a2998921078..9c568f71577f 100644 --- a/src/Core/src/Platform/iOS/MauiLabel.cs +++ b/src/Core/src/Platform/iOS/MauiLabel.cs @@ -83,7 +83,17 @@ 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 + return AddInsets(new Size() + { + Width = nfloat.Min(requestedSize.Width, size.Width), + Height = nfloat.Min(requestedSize.Height, size.Height), + }); + } SizeF AddInsets(SizeF size) => new SizeF( width: size.Width + TextInsets.Left + TextInsets.Right,