Skip to content

Commit

Permalink
[net7.0] Make iOS Label HTML behavior compatible with Forms (#11966)
Browse files Browse the repository at this point in the history
* Make iOS Label HTML behavior compatible with Forms Fixes #4230

* Fix usings

Co-authored-by: E.Z. Hart <hartez@gmail.com>
  • Loading branch information
github-actions[bot] and hartez authored Dec 8, 2022
1 parent e0daff2 commit b644a98
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Controls/src/Core/HandlerImpl/Label/Label.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public static void MapFont(ILabelHandler handler, Label label)
if (label?.HasFormattedTextSpans ?? false)
return;

if (label?.TextType == TextType.Html)
if (label?.TextType == TextType.Html && FontIsDefault(label))
{
// If no explicit font has been specified and we're displaying HTML,
// let the HTML determine the font
return;
}

Expand All @@ -81,8 +83,10 @@ public static void MapTextColor(ILabelHandler handler, Label label)
if (label?.HasFormattedTextSpans ?? false)
return;

if (label?.TextType == TextType.Html)
if (label?.TextType == TextType.Html && label.GetValue(TextColorProperty) == null)
{
// If no explicit text color has been specified and we're displaying HTML,
// let the HTML determine the colors
return;
}

Expand All @@ -98,5 +102,25 @@ public static void MapMaxLines(ILabelHandler handler, Label label)
{
handler.PlatformView?.UpdateMaxLines(label);
}

static bool FontIsDefault(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;
}
}
}
88 changes: 88 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Threading.Tasks;
#if __IOS__
using Foundation;
#endif
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand Down Expand Up @@ -362,5 +366,89 @@ await InvokeOnMainThreadAsync(async () =>
await normalBitmap.AssertEqual(formattedBitmap);
});
}

[Fact]
public async Task TextColorAppliesEvenInHtmlMode()
{
// Note: this is specifically a Controls-level rule that's inherited from Forms
// There's no reason other SDKs need to force a TextColor property when dealing
// with HTML text (since HTML text has its own color handling)

var label = new Label
{
TextType = TextType.Html,
TextColor = Colors.Red,
Text = "<p>Test</p>"
};

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<LabelHandler>(label);
Assert.Equal(Colors.Red, TextColor(handler));
});
}

[Fact]
public async Task FontStuffAppliesEvenInHtmlMode()
{
// 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,
FontSize = 64,
FontFamily = "Baskerville",
Text = "<p>Test</p>"
};

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<LabelHandler>(label);
AssertEquivalentFont(handler, label.ToFont());
});
}

Color TextColor(LabelHandler handler)
{
#if __IOS__
return GetPlatformLabel(handler).TextColor.ToColor();
#elif __ANDROID__
return ((uint)GetPlatformLabel(handler).CurrentTextColor).ToColor();
#elif WINDOWS
return (GetPlatformLabel(handler).Foreground as UI.Xaml.Media.SolidColorBrush).Color.ToColor();
#endif
}

static void AssertEquivalentFont(LabelHandler handler, Font font)
{
var fontManager = (IFontManager)handler.MauiContext.Services.GetService(typeof(IFontManager));

#if __IOS__
var targetTypeface = fontManager.GetFont(font);
var platformTypeface = handler.PlatformView.AttributedText.GetUIKitAttributes(0, out NSRange range).Font;
var targetFontSize = fontManager.GetFont(font).PointSize;

Assert.Equal(targetTypeface, platformTypeface);
Assert.Equal(targetFontSize, platformTypeface.PointSize);
#elif __ANDROID__
var targetTypeface = fontManager.GetTypeface(font);
var targetFontSize = handler.MauiContext.Context.ToPixels(fontManager.GetFontSize(font).Value);
var platformTypeface = handler.PlatformView.Typeface;
var platformFontSize = handler.PlatformView.TextSize;

Assert.Equal(targetTypeface, platformTypeface);
Assert.Equal(targetFontSize, platformFontSize);
#elif WINDOWS
var targetFontStyle = font.ToFontStyle();
var targetFontWeight = font.ToFontWeight();
var platformFontStyle = handler.PlatformView.FontStyle;
var platformFontWeight = handler.PlatformView.FontWeight;

Assert.Equal(targetFontStyle, platformFontStyle);
Assert.Equal(targetFontWeight, platformFontWeight);
#endif
}
}
}

0 comments on commit b644a98

Please sign in to comment.