From 2d6a0b81caf8152c4ccb12d83fd03a83f843b11a Mon Sep 17 00:00:00 2001 From: Jonathan Dick Date: Mon, 4 Apr 2022 20:24:36 -0400 Subject: [PATCH] Use fallbacks for font sizes when getting font from Span on Android (#5680) Spans have a 0 font size by default, and on Android this means the font is literally 0 in size and won't appear. If you had an attribute set other than None, it was hitting a code path here to get the font from the span and used the 0 size incorrectly. This should ensure the font size falls back to first the Formatted string or target control the font is to be used in, and then to the system default font size from the FontManager if necessary so that the span shows up. This fixes the android part of #5576 Co-authored-by: Rui Marinho --- src/Controls/src/Core/FontExtensions.cs | 10 ++++++++-- .../Android/Extensions/FormattedStringExtensions.cs | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Controls/src/Core/FontExtensions.cs b/src/Controls/src/Core/FontExtensions.cs index 8e4899490d7e..538a78534fa7 100644 --- a/src/Controls/src/Core/FontExtensions.cs +++ b/src/Controls/src/Core/FontExtensions.cs @@ -26,7 +26,13 @@ public static FontAttributes GetFontAttributes(this Font font) return attributes; } - public static Font ToFont(this IFontElement element) => - Font.OfSize(element.FontFamily, element.FontSize, enableScaling: element.FontAutoScalingEnabled).WithAttributes(element.FontAttributes); + public static Font ToFont(this IFontElement element, double? defaultSize = null) + { + var size = element.FontSize; + if (defaultSize.HasValue && (size == 0 || size == double.NaN)) + size = defaultSize.Value; + + return Font.OfSize(element.FontFamily, size, enableScaling: element.FontAutoScalingEnabled).WithAttributes(element.FontAttributes); + } } } diff --git a/src/Controls/src/Core/Platform/Android/Extensions/FormattedStringExtensions.cs b/src/Controls/src/Core/Platform/Android/Extensions/FormattedStringExtensions.cs index c330b9868a5e..7e6b66d84b7c 100644 --- a/src/Controls/src/Core/Platform/Android/Extensions/FormattedStringExtensions.cs +++ b/src/Controls/src/Core/Platform/Android/Extensions/FormattedStringExtensions.cs @@ -22,6 +22,8 @@ public static SpannableString ToSpannableString(this FormattedString formattedSt if (formattedString == null) return new SpannableString(string.Empty); + var defaultFontSize = defaultFont?.Size ?? fontManager.DefaultFontSize; + var builder = new StringBuilder(); for (int i = 0; i < formattedString.Spans.Count; i++) { @@ -70,7 +72,7 @@ public static SpannableString ToSpannableString(this FormattedString formattedSt spannable.SetSpan(new LineHeightSpan(textPaint, lineHeight), start, end, SpanTypes.InclusiveExclusive); } - var font = span.ToFont(); + var font = span.ToFont(defaultFontSize); if (font.IsDefault && defaultFont.HasValue) font = defaultFont.Value;