-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Implement Padding in ILabel Handlers #421
Changes from all commits
6780871
35591a1
b857b49
08fcc2a
c2bc3f0
b610353
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ namespace Microsoft.Maui | |
{ | ||
public interface ILabel : IView, IText, IFont | ||
{ | ||
Thickness Padding { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adds the property to the interface. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a mapping method to the Android aspect of the handler. |
||
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."); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a mapping method to the Standard aspect of the handler; the Standard aspect is just a placeholder and is never actually called. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ public partial class LabelHandler | |
[nameof(ILabel.FontFamily)] = MapFontFamily, | ||
[nameof(ILabel.FontSize)] = MapFontSize, | ||
[nameof(ILabel.FontAttributes)] = MapFontAttributes, | ||
[nameof(ILabel.Padding)] = MapPadding, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the mapping to the PropertyMapper for the handler. |
||
}; | ||
|
||
public LabelHandler() : base(LabelMapper) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ILabel, UILabel> | ||
public partial class LabelHandler : AbstractViewHandler<ILabel, MauiLabel> | ||
{ | ||
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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a mapping method to the iOS aspect of the handler. |
||
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."); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an extension method to handle the actual update for the native Android control. This logic is ported directly from the Forms Label renderer for Android. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,10 @@ public static void UpdateTextColor(this object nothing, ILabel label) | |
{ | ||
|
||
} | ||
|
||
public static void UpdatePadding(this object nothing, ILabel label) | ||
{ | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The placeholder extension method for Standard; this is never actually called. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an extension method to handle the actual update for the native iOS control. This logic is ported directly from the Forms Label renderer for iOS. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this particular property, we needed to extend the UILabel class with some custom logic because Padding doesn't exist natively. This class was ported from the Forms iOS platform project. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adds an on-device test to verify that the property is being set on the native Android control. This test will be run on an Android device as part of the CI process. |
||
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adds an on-device test to verify that the property is being set on the native iOS control. This test will be run on an iOS device as part of the CI process. |
||
UILabel GetNativeLabel(LabelHandler labelHandler) => | ||
(UILabel)labelHandler.View; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stub classes are implementations of the MAUI interfaces used for on-device testing. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adds an example of the property to the sample project.