diff --git a/src/Controls/src/Core/HandlerImpl/Label/Label.Android.cs b/src/Controls/src/Core/HandlerImpl/Label/Label.Android.cs index 33bb690ef6f1..473617315d81 100644 --- a/src/Controls/src/Core/HandlerImpl/Label/Label.Android.cs +++ b/src/Controls/src/Core/HandlerImpl/Label/Label.Android.cs @@ -33,14 +33,14 @@ private protected override void OnHandlerChangedCore() } } - public static void MapTextType(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); + public static void MapTextType(LabelHandler handler, Label label) => MapTextType((ILabelHandler)handler, label); public static void MapText(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); public static void MapLineBreakMode(LabelHandler handler, Label label) => MapLineBreakMode((ILabelHandler)handler, label); public static void MapTextType(ILabelHandler handler, Label label) { - Platform.TextViewExtensions.UpdateText(handler.PlatformView, label); + handler.UpdateValue(nameof(ILabel.Text)); } public static void MapText(ILabelHandler handler, Label label) @@ -48,17 +48,6 @@ public static void MapText(ILabelHandler handler, Label label) Platform.TextViewExtensions.UpdateText(handler.PlatformView, label); } - // TODO: NET8 make this public - internal static void MapTextColor(ILabelHandler handler, Label label) - { - handler.PlatformView?.UpdateTextColor(label); - - if (label?.HasFormattedTextSpans ?? false) - return; - - Platform.TextViewExtensions.UpdateText(handler.PlatformView, label); - } - public static void MapLineBreakMode(ILabelHandler handler, Label label) { handler.PlatformView?.UpdateLineBreakMode(label); diff --git a/src/Controls/src/Core/HandlerImpl/Label/Label.Tizen.cs b/src/Controls/src/Core/HandlerImpl/Label/Label.Tizen.cs index 2150ad58740a..1670dcfbef7b 100644 --- a/src/Controls/src/Core/HandlerImpl/Label/Label.Tizen.cs +++ b/src/Controls/src/Core/HandlerImpl/Label/Label.Tizen.cs @@ -7,12 +7,12 @@ namespace Microsoft.Maui.Controls { public partial class Label { - public static void MapTextType(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); + public static void MapTextType(LabelHandler handler, Label label) => MapTextType((ILabelHandler)handler, label); public static void MapText(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); public static void MapTextType(ILabelHandler handler, Label label) { - Platform.TextExtensions.UpdateText(handler.PlatformView, label); + handler.UpdateValue(nameof(ILabel.Text)); } public static void MapText(ILabelHandler handler, Label label) diff --git a/src/Controls/src/Core/HandlerImpl/Label/Label.Windows.cs b/src/Controls/src/Core/HandlerImpl/Label/Label.Windows.cs index 3fd13da10794..8e9c37f84c4b 100644 --- a/src/Controls/src/Core/HandlerImpl/Label/Label.Windows.cs +++ b/src/Controls/src/Core/HandlerImpl/Label/Label.Windows.cs @@ -14,7 +14,7 @@ public static void MapDetectReadingOrderFromContent(ILabelHandler handler, Label Platform.TextBlockExtensions.UpdateDetectReadingOrderFromContent(handler.PlatformView, label); public static void MapTextType(ILabelHandler handler, Label label) => - Platform.TextBlockExtensions.UpdateText(handler.PlatformView, label); + handler.UpdateValue(nameof(ILabel.Text)); public static void MapText(ILabelHandler handler, Label label) => Platform.TextBlockExtensions.UpdateText(handler.PlatformView, label); diff --git a/src/Controls/src/Core/HandlerImpl/Label/Label.cs b/src/Controls/src/Core/HandlerImpl/Label/Label.cs index 85cf050ed89a..5b6bb3429825 100644 --- a/src/Controls/src/Core/HandlerImpl/Label/Label.cs +++ b/src/Controls/src/Core/HandlerImpl/Label/Label.cs @@ -15,9 +15,6 @@ public partial class Label #if WINDOWS [PlatformConfiguration.WindowsSpecific.InputView.DetectReadingOrderFromContentProperty.PropertyName] = MapDetectReadingOrderFromContent, #endif -#if ANDROID - [nameof(TextColor)] = MapTextColor, -#endif #if IOS [nameof(TextDecorations)] = MapTextDecorations, [nameof(CharacterSpacing)] = MapCharacterSpacing, diff --git a/src/Controls/src/Core/HandlerImpl/Label/Label.iOS.cs b/src/Controls/src/Core/HandlerImpl/Label/Label.iOS.cs index 94b65a4e4aa6..e8a2bd4c41a9 100644 --- a/src/Controls/src/Core/HandlerImpl/Label/Label.iOS.cs +++ b/src/Controls/src/Core/HandlerImpl/Label/Label.iOS.cs @@ -1,12 +1,12 @@ #nullable disable +using System; using Microsoft.Maui.Controls.Platform; -using Microsoft.Maui.Handlers; namespace Microsoft.Maui.Controls { public partial class Label { - public static void MapTextType(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); + public static void MapTextType(LabelHandler handler, Label label) => MapTextType((ILabelHandler)handler, label); public static void MapText(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); public static void MapCharacterSpacing(LabelHandler handler, Label label) => MapCharacterSpacing((ILabelHandler)handler, label); public static void MapTextDecorations(LabelHandler handler, Label label) => MapTextDecorations((ILabelHandler)handler, label); @@ -17,61 +17,48 @@ public partial class Label public static void MapTextType(ILabelHandler handler, Label label) { - Platform.LabelExtensions.UpdateText(handler.PlatformView, label); + handler.UpdateValue(nameof(ILabel.Text)); } public static void MapText(ILabelHandler handler, Label label) { Platform.LabelExtensions.UpdateText(handler.PlatformView, label); + + MapFormatting(handler, label); } public static void MapTextDecorations(ILabelHandler handler, Label label) { - if (label?.HasFormattedTextSpans ?? false) - return; - - if (label?.TextType == TextType.Html) - { + if (!IsPlainText(label)) return; - } LabelHandler.MapTextDecorations(handler, label); } public static void MapCharacterSpacing(ILabelHandler handler, Label label) { - if (label?.HasFormattedTextSpans ?? false) - return; - - if (label?.TextType == TextType.Html) - { + if (!IsPlainText(label)) return; - } LabelHandler.MapCharacterSpacing(handler, label); } public static void MapLineHeight(ILabelHandler handler, Label label) { - if (label?.HasFormattedTextSpans ?? false) - return; - - if (label?.TextType == TextType.Html) - { + if (!IsPlainText(label)) return; - } LabelHandler.MapLineHeight(handler, label); } public static void MapFont(ILabelHandler handler, Label label) { - if (label?.HasFormattedTextSpans ?? false) + if (label.HasFormattedTextSpans) return; - if (label?.TextType == TextType.Html && FontIsDefault(label)) + if (label.TextType == TextType.Html && IsDefaultFont(label)) { - // If no explicit font has been specified and we're displaying HTML, + // If no explicit font has been specified and we're displaying HTML, // let the HTML determine the font return; } @@ -81,12 +68,12 @@ public static void MapFont(ILabelHandler handler, Label label) public static void MapTextColor(ILabelHandler handler, Label label) { - if (label?.HasFormattedTextSpans ?? false) + if (label.HasFormattedTextSpans) return; - if (label?.TextType == TextType.Html && label.GetValue(TextColorProperty) == null) + if (label.TextType == TextType.Html && label.TextColor.IsDefault()) { - // If no explicit text color has been specified and we're displaying HTML, + // If no explicit text color has been specified and we're displaying HTML, // let the HTML determine the colors return; } @@ -104,22 +91,33 @@ public static void MapMaxLines(ILabelHandler handler, Label label) handler.PlatformView?.UpdateMaxLines(label); } - static bool FontIsDefault(Label label) + static void MapFormatting(ILabelHandler handler, Label label) + { + handler.UpdateValue(nameof(ILabel.TextColor)); + handler.UpdateValue(nameof(ILabel.Font)); + } + + static bool IsDefaultFont(Label label) { if (label.IsSet(Label.FontAttributesProperty)) - { return false; - } if (label.IsSet(Label.FontFamilyProperty)) - { return false; - } if (label.IsSet(Label.FontSizeProperty)) - { return false; - } + + return true; + } + + static bool IsPlainText(Label label) + { + if (label.HasFormattedTextSpans) + return false; + + if (label.TextType != TextType.Text) + return false; return true; } diff --git a/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs b/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs index e0bef53595c9..e0cd0c62a0ee 100644 --- a/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs @@ -410,6 +410,50 @@ await InvokeOnMainThreadAsync(() => }); } + [Fact] + public async Task TextTypeAfterFontStuffIsCorrect() + { + // Note: this is specifically a Controls-level rule that's inherited from Forms + // There's no reason other SDKs need to force font properties when dealing + // with HTML text (since HTML can do that on its own) + + var label = new Label + { + FontSize = 64, + FontFamily = "Baskerville", + Text = "

Test

" + }; + + await InvokeOnMainThreadAsync(() => + { + var handler = CreateHandler(label); + label.TextType = TextType.Html; + AssertEquivalentFont(handler, label.ToFont()); + }); + } + + [Fact] + public async Task FontStuffAfterTextTypeIsCorrect() + { + // Note: this is specifically a Controls-level rule that's inherited from Forms + // There's no reason other SDKs need to force font properties when dealing + // with HTML text (since HTML can do that on its own) + + var label = new Label + { + TextType = TextType.Html, + Text = "

Test

" + }; + + await InvokeOnMainThreadAsync(() => + { + var handler = CreateHandler(label); + label.FontFamily = "Baskerville"; + label.FontSize = 64; + AssertEquivalentFont(handler, label.ToFont()); + }); + } + Color TextColor(LabelHandler handler) { #if __IOS__ diff --git a/src/Core/src/Handlers/Label/LabelHandler.iOS.cs b/src/Core/src/Handlers/Label/LabelHandler.iOS.cs index 405dc5016577..4ce9126859c2 100644 --- a/src/Core/src/Handlers/Label/LabelHandler.iOS.cs +++ b/src/Core/src/Handlers/Label/LabelHandler.iOS.cs @@ -73,13 +73,13 @@ public static void MapLineHeight(ILabelHandler handler, ILabel label) public static void MapFormatting(ILabelHandler handler, ILabel label) { // Update all of the attributed text formatting properties - handler.PlatformView?.UpdateLineHeight(label); - handler.PlatformView?.UpdateTextDecorations(label); - handler.PlatformView?.UpdateCharacterSpacing(label); + handler.UpdateValue(nameof(ILabel.LineHeight)); + handler.UpdateValue(nameof(ILabel.TextDecorations)); + handler.UpdateValue(nameof(ILabel.CharacterSpacing)); // Setting any of those may have removed text alignment settings, // so we need to make sure those are applied, too - handler.PlatformView?.UpdateHorizontalTextAlignment(label); + handler.UpdateValue(nameof(ILabel.HorizontalTextAlignment)); } } } \ No newline at end of file