diff --git a/src/Compatibility/Core/src/Android/Renderers/FontExtensions.cs b/src/Compatibility/Core/src/Android/Renderers/FontExtensions.cs index a3f1d0527d4c..968011bd8fbe 100644 --- a/src/Compatibility/Core/src/Android/Renderers/FontExtensions.cs +++ b/src/Compatibility/Core/src/Android/Renderers/FontExtensions.cs @@ -22,7 +22,9 @@ internal static Typeface ToTypeface(this IFontElement self) if (self.IsDefault()) return Forms.FontManager.DefaultTypeface; - return Forms.FontManager.GetTypeface(Font.OfSize(self.FontFamily, self.FontSize).WithAttributes(self.FontAttributes)); + var font = Font.OfSize(self.FontFamily, self.FontSize).WithAttributes(self.FontAttributes); + + return Forms.FontManager.GetTypeface(font); } } } \ No newline at end of file diff --git a/src/Controls/src/Core/Button.cs b/src/Controls/src/Core/Button.cs index 8a6e982b572d..c47acc12f483 100644 --- a/src/Controls/src/Core/Button.cs +++ b/src/Controls/src/Core/Button.cs @@ -235,19 +235,24 @@ protected override void OnBindingContextChanged() } void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontSizeChanged(double oldValue, double newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); double IFontElement.FontSizeDefaultValueCreator() => Device.GetNamedSize(NamedSize.Default, (Button)this); void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontChanged(Font oldValue, Font newValue) => + HandleFontChanged(); + + void HandleFontChanged() + { InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + } Aspect IImageElement.Aspect => Aspect.AspectFit; ImageSource IImageElement.Source => ImageSource; diff --git a/src/Controls/src/Core/Entry.cs b/src/Controls/src/Core/Entry.cs index 9fa247b8179d..42d4d83f6f1d 100644 --- a/src/Controls/src/Core/Entry.cs +++ b/src/Controls/src/Core/Entry.cs @@ -133,16 +133,23 @@ double IFontElement.FontSizeDefaultValueCreator() => Device.GetNamedSize(NamedSize.Default, (Entry)this); void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontSizeChanged(double oldValue, double newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontChanged(Font oldValue, Font newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); + + void HandleFontChanged() + { + // Null out the Maui font value so it will be recreated next time it's accessed + _font = null; + InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + } public event EventHandler Completed; diff --git a/src/Controls/src/Core/HandlerImpl/Button.Impl.cs b/src/Controls/src/Core/HandlerImpl/Button.Impl.cs index cf8a49f5ecc4..da7df1b858a4 100644 --- a/src/Controls/src/Core/HandlerImpl/Button.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Button.Impl.cs @@ -16,5 +16,7 @@ void IButton.Released() { (this as IButtonController).SendReleased(); } + + Font IText.Font => Font; } } \ No newline at end of file diff --git a/src/Controls/src/Core/HandlerImpl/Entry.Impl.cs b/src/Controls/src/Core/HandlerImpl/Entry.Impl.cs index c9863bb75c6b..2bb17780407b 100644 --- a/src/Controls/src/Core/HandlerImpl/Entry.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Entry.Impl.cs @@ -2,6 +2,19 @@ { public partial class Entry : IEntry { + Font? _font; + Font IText.Font + { + get + { + if (_font == null) + { + _font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes); + } + + return _font.Value; + } + } } } \ No newline at end of file diff --git a/src/Controls/src/Core/HandlerImpl/Label.Impl.cs b/src/Controls/src/Core/HandlerImpl/Label.Impl.cs index de323ea90e9c..1ce5f752ec35 100644 --- a/src/Controls/src/Core/HandlerImpl/Label.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Label.Impl.cs @@ -2,6 +2,19 @@ namespace Microsoft.Maui.Controls { public partial class Label : ILabel { + Font? _font; + Font IText.Font + { + get + { + if (_font == null) + { + _font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes); + } + + return _font.Value; + } + } } } \ No newline at end of file diff --git a/src/Controls/src/Core/Label.cs b/src/Controls/src/Core/Label.cs index 1d423cb47111..1cd028c6c5f2 100644 --- a/src/Controls/src/Core/Label.cs +++ b/src/Controls/src/Core/Label.cs @@ -199,16 +199,23 @@ double IFontElement.FontSizeDefaultValueCreator() => Device.GetNamedSize(NamedSize.Default, (Label)this); void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontSizeChanged(double oldValue, double newValue) => - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + HandleFontChanged(); void IFontElement.OnFontChanged(Font oldValue, Font newValue) => + HandleFontChanged(); + + void HandleFontChanged() + { + // Null out the Maui font value so it will be recreated next time it's accessed + _font = null; InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + } void ILineHeightElement.OnLineHeightChanged(double oldValue, double newValue) => InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); diff --git a/src/Core/src/Core/IFont.cs b/src/Core/src/Core/IFont.cs deleted file mode 100644 index 09b8cf32c58f..000000000000 --- a/src/Core/src/Core/IFont.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Microsoft.Maui -{ - /// - /// Provides functionality to be able to customize fonts. - /// - public interface IFont - { - /// - /// Describe font styles (Bold, Italic, None). - /// - FontAttributes FontAttributes { get; } - - /// - /// Gets the font family of the font. - /// - string FontFamily { get; } - - /// - /// Gets the size of the font. - /// - double FontSize { get; } - } -} \ No newline at end of file diff --git a/src/Core/src/Core/ILabel.cs b/src/Core/src/Core/ILabel.cs index 76bb642e05a7..6e18f63c1150 100644 --- a/src/Core/src/Core/ILabel.cs +++ b/src/Core/src/Core/ILabel.cs @@ -3,7 +3,7 @@ namespace Microsoft.Maui /// /// Represents a View that displays text. /// - public interface ILabel : IView, IText, IFont + public interface ILabel : IView, IText { /// /// Gets the space between the text of the Label and it's border. diff --git a/src/Core/src/Core/IText.cs b/src/Core/src/Core/IText.cs index c56e9b634afe..8b185ec1ba57 100644 --- a/src/Core/src/Core/IText.cs +++ b/src/Core/src/Core/IText.cs @@ -14,5 +14,10 @@ public interface IText /// Gets the text color. /// Color TextColor { get; } + + /// + /// Gets the font family, style and size of the font. + /// + Font Font { 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 d0312b896710..2781f8c04b57 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.Android.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.Android.cs @@ -1,7 +1,6 @@ using System; using Android.Widget; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Maui; namespace Microsoft.Maui.Handlers { @@ -32,30 +31,16 @@ public static void MapTextColor(LabelHandler handler, ILabel label) { handler.TypedNativeView?.UpdateTextColor(label, DefaultTextColor); } - - public static void MapFontFamily(LabelHandler handler, ILabel label) - { - MapFont(handler, label); - } - - public static void MapFontSize(LabelHandler handler, ILabel label) - { - MapFont(handler, label); - } - - public static void MapFontAttributes(LabelHandler handler, ILabel label) - { - MapFont(handler, label); - } - - public static void MapPadding(LabelHandler handler, ILabel label) + + public static void MapPadding(LabelHandler handler, ILabel label) { handler.TypedNativeView?.UpdatePadding(label); } - static void MapFont(LabelHandler handler, ILabel label) + public 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."); + var services = App.Current?.Services + ?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null."); var fontManager = services.GetRequiredService(); handler.TypedNativeView?.UpdateFont(label, fontManager); diff --git a/src/Core/src/Handlers/Label/LabelHandler.Standard.cs b/src/Core/src/Handlers/Label/LabelHandler.Standard.cs index 017b18a4d53c..5209b48ea011 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.Standard.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.Standard.cs @@ -8,9 +8,7 @@ public partial class LabelHandler : AbstractViewHandler public static void MapText(IViewHandler handler, ILabel label) { } 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) { } + public static void MapFont(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 6e52a816bf40..2dfb63a4cb6a 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.cs @@ -6,9 +6,7 @@ public partial class LabelHandler { [nameof(ILabel.TextColor)] = MapTextColor, [nameof(ILabel.Text)] = MapText, - [nameof(ILabel.FontFamily)] = MapFontFamily, - [nameof(ILabel.FontSize)] = MapFontSize, - [nameof(ILabel.FontAttributes)] = MapFontAttributes, + [nameof(ILabel.Font)] = MapFont, [nameof(ILabel.Padding)] = MapPadding, }; diff --git a/src/Core/src/Handlers/Label/LabelHandler.iOS.cs b/src/Core/src/Handlers/Label/LabelHandler.iOS.cs index cd4bb98ff57e..deabd16ead05 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.iOS.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.iOS.cs @@ -19,29 +19,15 @@ public static void MapTextColor(LabelHandler handler, ILabel label) handler.TypedNativeView?.UpdateTextColor(label); } - public static void MapFontFamily(LabelHandler handler, ILabel label) - { - MapFont(handler, label); - } - - public static void MapFontSize(LabelHandler handler, ILabel label) - { - MapFont(handler, label); - } - - 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) + public 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."); + var services = App.Current?.Services ?? + throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null."); var fontManager = services.GetRequiredService(); handler.TypedNativeView?.UpdateFont(label, fontManager); diff --git a/src/Core/src/Platform/Android/LabelExtensions.cs b/src/Core/src/Platform/Android/LabelExtensions.cs index e3921644b625..f4d6f8e04616 100644 --- a/src/Core/src/Platform/Android/LabelExtensions.cs +++ b/src/Core/src/Platform/Android/LabelExtensions.cs @@ -26,7 +26,7 @@ public static void UpdateTextColor(this TextView textView, ILabel label, Color d public static void UpdateFont(this TextView textView, ILabel label, IFontManager fontManager) { - var font = label.GetFont(); + var font = label.Font; var tf = fontManager.GetTypeface(font); textView.Typeface = tf; diff --git a/src/Core/src/Platform/FontExtensions.cs b/src/Core/src/Platform/FontExtensions.cs deleted file mode 100644 index f1d0ae3f6098..000000000000 --- a/src/Core/src/Platform/FontExtensions.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Microsoft.Maui -{ - internal static class FontExtensions - { - public static Font GetFont(this IFont font) => - Font.OfSize(font.FontFamily, font.FontSize).WithAttributes(font.FontAttributes); - } -} diff --git a/src/Core/src/Platform/iOS/LabelExtensions.cs b/src/Core/src/Platform/iOS/LabelExtensions.cs index ffa4ab84a214..fe63f78fbd82 100644 --- a/src/Core/src/Platform/iOS/LabelExtensions.cs +++ b/src/Core/src/Platform/iOS/LabelExtensions.cs @@ -27,9 +27,7 @@ public static void UpdateTextColor(this UILabel nativeLabel, ILabel label) public static void UpdateFont(this UILabel nativeLabel, ILabel label, IFontManager fontManager) { - var font = label.GetFont(); - - var uiFont = fontManager.GetFont(font); + var uiFont = fontManager.GetFont(label.Font); nativeLabel.Font = uiFont; } diff --git a/src/Core/src/Primitives/Font.cs b/src/Core/src/Primitives/Font.cs index 148fff3711c1..82a626fa5517 100644 --- a/src/Core/src/Primitives/Font.cs +++ b/src/Core/src/Primitives/Font.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; namespace Microsoft.Maui { diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs index 5d2a95f30a48..811cadf8e12e 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs @@ -18,7 +18,7 @@ public async Task FontFamilyInitializesCorrectly(string family) var label = new LabelStub() { Text = "Test", - FontFamily = family + Font = Font.OfSize(family, 10) }; var handler = await CreateHandlerAsync(label); diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs index 62c10afaf468..520088061d97 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs @@ -57,10 +57,10 @@ public async Task FontSizeInitializesCorrectly(int fontSize) var label = new LabelStub() { Text = "Test", - FontSize = fontSize + Font = Font.OfSize("Arial", fontSize) }; - await ValidatePropertyInitValue(label, () => label.FontSize, GetNativeUnscaledFontSize, label.FontSize); + await ValidatePropertyInitValue(label, () => label.Font.FontSize, GetNativeUnscaledFontSize, label.Font.FontSize); } [Theory(DisplayName = "Font Attributes Initialize Correctly")] @@ -73,11 +73,11 @@ public async Task AttributesInitializeCorrectly(FontAttributes attributes, bool var label = new LabelStub() { Text = "Test", - FontAttributes = attributes + Font = Font.OfSize("Arial", 10).WithAttributes(attributes) }; - await ValidatePropertyInitValue(label, () => label.FontAttributes.HasFlag(FontAttributes.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(label, () => label.FontAttributes.HasFlag(FontAttributes.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(label, () => label.Font.FontAttributes.HasFlag(FontAttributes.Bold), GetNativeIsBold, isBold); + await ValidatePropertyInitValue(label, () => label.Font.FontAttributes.HasFlag(FontAttributes.Italic), GetNativeIsItalic, isItalic); } } } \ 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 b0e5abcb2661..a151ab71923d 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs @@ -19,7 +19,7 @@ public async Task FontFamilyInitializesCorrectly(string family) var label = new LabelStub() { Text = "Test", - FontFamily = family + Font = Font.OfSize(family, 10) }; var nativeFont = await GetValueAsync(label, handler => GetNativeLabel(handler).Font); diff --git a/src/Core/tests/DeviceTests/Stubs/ButtonStub.cs b/src/Core/tests/DeviceTests/Stubs/ButtonStub.cs index 5fb0b56bd7e9..2ffddd68e50e 100644 --- a/src/Core/tests/DeviceTests/Stubs/ButtonStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/ButtonStub.cs @@ -1,5 +1,4 @@ using System; -using Microsoft.Maui; namespace Microsoft.Maui.DeviceTests.Stubs { @@ -9,6 +8,8 @@ public partial class ButtonStub : StubBase, IButton public Color TextColor { get; set; } + public Font Font { get; set; } + public event EventHandler Pressed; public event EventHandler Released; public event EventHandler Clicked; diff --git a/src/Core/tests/DeviceTests/Stubs/EntryStub.cs b/src/Core/tests/DeviceTests/Stubs/EntryStub.cs index b618d6ee97e3..ab2c1ab732e7 100644 --- a/src/Core/tests/DeviceTests/Stubs/EntryStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/EntryStub.cs @@ -26,5 +26,7 @@ public string Text void OnTextChanged(string oldValue, string newValue) => TextChanged?.Invoke(this, new StubPropertyChangedEventArgs(oldValue, newValue)); + + public Font Font { get; set; } } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Stubs/LabelStub.cs b/src/Core/tests/DeviceTests/Stubs/LabelStub.cs index b3bb62a1283a..e226806bb7a2 100644 --- a/src/Core/tests/DeviceTests/Stubs/LabelStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/LabelStub.cs @@ -6,12 +6,8 @@ public partial class LabelStub : StubBase, ILabel public Color TextColor { get; set; } - public FontAttributes FontAttributes { get; set; } - - public string FontFamily { get; set; } - - public double FontSize { get; set; } public Thickness Padding { get; set; } + public Font Font { get; set; } } } \ No newline at end of file diff --git a/src/Core/tests/UnitTests/TestClasses/ButtonStub.cs b/src/Core/tests/UnitTests/TestClasses/ButtonStub.cs index 207156147847..a34361b4672c 100644 --- a/src/Core/tests/UnitTests/TestClasses/ButtonStub.cs +++ b/src/Core/tests/UnitTests/TestClasses/ButtonStub.cs @@ -13,5 +13,7 @@ public void Clicked() { } public void Pressed() { } public void Released() { } + + public Font Font { get; set; } } } \ No newline at end of file