Skip to content

Commit

Permalink
Use fallbacks for font sizes when getting font from Span on Android (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
Redth and rmarinho authored Apr 5, 2022
1 parent 4d9c3a4 commit 2d6a0b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Controls/src/Core/FontExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 2d6a0b8

Please sign in to comment.