diff --git a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs index 7a2562968cb5..b92542fe1519 100644 --- a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs +++ b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs @@ -33,6 +33,7 @@ void SetupMauiLayout() verticalStack.Add(new Label { Text = "This should be BIG text!", FontSize = 24 }); verticalStack.Add(new Label { Text = "This should be BOLD text!", FontAttributes = FontAttributes.Bold }); verticalStack.Add(new Label { Text = "This should be a CUSTOM font!", FontFamily = "Dokdo" }); + verticalStack.Add(new Label { Text = "This should have padding", Padding = new Thickness(40), BackgroundColor = Color.LightBlue }); var button = new Button() { Text = _viewModel.Text, WidthRequest = 200 }; var button2 = new Button() diff --git a/src/Core/src/Core/ILabel.cs b/src/Core/src/Core/ILabel.cs index b581869c235e..a12ae569f831 100644 --- a/src/Core/src/Core/ILabel.cs +++ b/src/Core/src/Core/ILabel.cs @@ -2,5 +2,6 @@ namespace Microsoft.Maui { public interface ILabel : IView, IText, IFont { + Thickness Padding { get; } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/Label/LabelHandler.Android.cs b/src/Core/src/Handlers/Label/LabelHandler.Android.cs index 3a65fbe03570..f8699b866280 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.Android.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.Android.cs @@ -48,6 +48,11 @@ public static void MapFontAttributes(LabelHandler handler, ILabel label) MapFont(handler, label); } + public static void MapPadding(LabelHandler handler, ILabel label) + { + handler.TypedNativeView?.UpdatePadding(label); + } + static void MapFont(LabelHandler handler, ILabel label) { var services = App.Current?.Services ?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null."); diff --git a/src/Core/src/Handlers/Label/LabelHandler.Standard.cs b/src/Core/src/Handlers/Label/LabelHandler.Standard.cs index f4f2035632a8..f023c6e87972 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.Standard.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.Standard.cs @@ -11,5 +11,6 @@ public static void MapTextColor(IViewHandler handler, ILabel label) { } public static void MapFontFamily(LabelHandler handler, ILabel label) { } public static void MapFontSize(LabelHandler handler, ILabel label) { } public static void MapFontAttributes(LabelHandler handler, ILabel label) { } + public static void MapPadding(LabelHandler handler, ILabel label) { } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/Label/LabelHandler.cs b/src/Core/src/Handlers/Label/LabelHandler.cs index 70ced6c495d8..6e52a816bf40 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.cs @@ -9,6 +9,7 @@ public partial class LabelHandler [nameof(ILabel.FontFamily)] = MapFontFamily, [nameof(ILabel.FontSize)] = MapFontSize, [nameof(ILabel.FontAttributes)] = MapFontAttributes, + [nameof(ILabel.Padding)] = MapPadding, }; public LabelHandler() : base(LabelMapper) diff --git a/src/Core/src/Handlers/Label/LabelHandler.iOS.cs b/src/Core/src/Handlers/Label/LabelHandler.iOS.cs index c5f347f7debc..cd4bb98ff57e 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.iOS.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.iOS.cs @@ -1,12 +1,13 @@ using System; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Maui.Platform.iOS; using UIKit; namespace Microsoft.Maui.Handlers { - public partial class LabelHandler : AbstractViewHandler + public partial class LabelHandler : AbstractViewHandler { - protected override UILabel CreateNativeView() => new UILabel(); + protected override MauiLabel CreateNativeView() => new MauiLabel(); public static void MapText(LabelHandler handler, ILabel label) { @@ -33,6 +34,11 @@ public static void MapFontAttributes(LabelHandler handler, ILabel label) MapFont(handler, label); } + public static void MapPadding(LabelHandler handler, ILabel label) + { + handler.TypedNativeView?.UpdatePadding(label); + } + static void MapFont(LabelHandler handler, ILabel label) { var services = App.Current?.Services ?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null."); diff --git a/src/Core/src/Platform/Android/LabelExtensions.cs b/src/Core/src/Platform/Android/LabelExtensions.cs index b351ccbf3831..43973b9d43dc 100644 --- a/src/Core/src/Platform/Android/LabelExtensions.cs +++ b/src/Core/src/Platform/Android/LabelExtensions.cs @@ -34,5 +34,21 @@ public static void UpdateFont(this TextView textView, ILabel label, IFontManager var sp = fontManager.GetScaledPixel(font); textView.SetTextSize(ComplexUnitType.Sp, sp); } + + public static void UpdatePadding(this TextView textView, ILabel label) + { + var context = textView.Context; + + if (context == null) + { + return; + } + + textView.SetPadding( + (int)context.ToPixels(label.Padding.Left), + (int)context.ToPixels(label.Padding.Top), + (int)context.ToPixels(label.Padding.Right), + (int)context.ToPixels(label.Padding.Bottom)); + } } } \ No newline at end of file diff --git a/src/Core/src/Platform/Standard/LabelExtensions.cs b/src/Core/src/Platform/Standard/LabelExtensions.cs index e437c7b917ce..9f06a1a067f2 100644 --- a/src/Core/src/Platform/Standard/LabelExtensions.cs +++ b/src/Core/src/Platform/Standard/LabelExtensions.cs @@ -11,5 +11,10 @@ public static void UpdateTextColor(this object nothing, ILabel label) { } + + public static void UpdatePadding(this object nothing, ILabel label) + { + + } } } \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/LabelExtensions.cs b/src/Core/src/Platform/iOS/LabelExtensions.cs index 6ead2ffaf349..9e95cf17b917 100644 --- a/src/Core/src/Platform/iOS/LabelExtensions.cs +++ b/src/Core/src/Platform/iOS/LabelExtensions.cs @@ -1,3 +1,4 @@ +using Microsoft.Maui.Platform.iOS; using UIKit; namespace Microsoft.Maui @@ -31,5 +32,15 @@ public static void UpdateFont(this UILabel nativeLabel, ILabel label, IFontManag var uiFont = fontManager.GetFont(font); nativeLabel.Font = uiFont; } + + public static void UpdatePadding(this MauiLabel nativeLabel, ILabel label) + { + nativeLabel.TextInsets = new UIEdgeInsets( + (float)label.Padding.Top, + (float)label.Padding.Left, + (float)label.Padding.Bottom, + (float)label.Padding.Right); + + } } } \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/MauiLabel.cs b/src/Core/src/Platform/iOS/MauiLabel.cs new file mode 100644 index 000000000000..1697752d6d6b --- /dev/null +++ b/src/Core/src/Platform/iOS/MauiLabel.cs @@ -0,0 +1,26 @@ +using CoreGraphics; +using UIKit; + +namespace Microsoft.Maui.Platform.iOS +{ + public class MauiLabel : UILabel + { + public UIEdgeInsets TextInsets { get; set; } + + public MauiLabel(CGRect frame) : base(frame) + { + } + + public MauiLabel() + { + } + + public override void DrawText(CGRect rect) => base.DrawText(TextInsets.InsetRect(rect)); + + public override CGSize SizeThatFits(CGSize size) => AddInsets(base.SizeThatFits(size)); + + CGSize AddInsets(CGSize size) => new CGSize( + width: size.Width + TextInsets.Left + TextInsets.Right, + height: size.Height + TextInsets.Top + TextInsets.Bottom); + } +} diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs index 5e9bf3000682..54beff46776a 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs @@ -36,6 +36,31 @@ public async Task FontFamilyInitializesCorrectly(string family) Assert.NotEqual(fontManager.DefaultTypeface, nativeLabel.Typeface); } + [Fact] + public async Task PaddingInitializesCorrectly() + { + var label = new LabelStub() + { + Text = "Test", + Padding = new Thickness(5, 10, 15, 20) + }; + + var handler = await CreateHandlerAsync(label); + var (left, top, right, bottom) = GetNativePadding((TextView)handler.NativeView); + + var context = handler.View.Context; + + var expectedLeft = context.ToPixels(5); + var expectedTop = context.ToPixels(10); + var expectedRight = context.ToPixels(15); + var expectedBottom = context.ToPixels(20); + + Assert.Equal(expectedLeft, left); + Assert.Equal(expectedTop, top); + Assert.Equal(expectedRight, right); + Assert.Equal(expectedBottom, bottom); + } + TextView GetNativeLabel(LabelHandler labelHandler) => (TextView)labelHandler.View; @@ -64,5 +89,10 @@ Task ValidateNativeBackgroundColor(ILabel label, Color color) return GetNativeLabel(CreateHandler(label)).AssertContainsColor(color); }); } + + (double left, double top, double right, double bottom) GetNativePadding(Android.Views.View view) + { + return (view.PaddingLeft, view.PaddingTop, view.PaddingRight, view.PaddingBottom); + } } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs index 6dedc2efcf74..b0e5abcb2661 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.Maui.Handlers; +using Microsoft.Maui.Platform.iOS; using UIKit; using Xunit; @@ -34,6 +35,24 @@ public async Task FontFamilyInitializesCorrectly(string family) Assert.NotEqual(fontManager.DefaultFont.FamilyName, nativeFont.FamilyName); } + [Fact] + public async Task PaddingInitializesCorrectly() + { + var label = new LabelStub() + { + Text = "Test", + Padding = new Thickness(5, 10, 15, 20) + }; + + var handler = await CreateHandlerAsync(label); + var insets = ((MauiLabel)handler.NativeView).TextInsets; + + Assert.Equal(5, insets.Left); + Assert.Equal(10, insets.Top); + Assert.Equal(15, insets.Right); + Assert.Equal(20, insets.Bottom); + } + UILabel GetNativeLabel(LabelHandler labelHandler) => (UILabel)labelHandler.View; diff --git a/src/Core/tests/DeviceTests/Stubs/LabelStub.cs b/src/Core/tests/DeviceTests/Stubs/LabelStub.cs index 47e75098c40a..b3bb62a1283a 100644 --- a/src/Core/tests/DeviceTests/Stubs/LabelStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/LabelStub.cs @@ -11,5 +11,7 @@ public partial class LabelStub : StubBase, ILabel public string FontFamily { get; set; } public double FontSize { get; set; } + + public Thickness Padding { get; set; } } } \ No newline at end of file