Skip to content
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

Make iOS Label HTML behavior compatible with Forms #11569

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}