Skip to content

Commit 778d523

Browse files
committed
Added Java native code
1 parent 24d4049 commit 778d523

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Controls/src/Core/Platform/Android/Extensions/FormattedStringExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ internal static SpannableString ToSpannableStringNewWay(
6464

6565
var builder = new StringBuilder();
6666

67-
var fontMetrics = context?.Resources?.DisplayMetrics != null ? new Paint()
68-
{
69-
TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, (float)defaultFontSize, context.Resources.DisplayMetrics)
70-
}.GetFontMetrics()
71-
: null;
72-
67+
var fontMetrics = PlatformInterop.GetFontMetrics(context, defaultFontSize);
68+
7369
for (int i = 0; i < formattedString.Spans.Count; i++)
7470
{
7571
Span span = formattedString.Spans[i];

src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/PlatformInterop.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,26 @@ public static Rect getCurrentWindowMetrics(Activity activity) {
591591
.getBounds();
592592
}
593593

594+
/**
595+
* Gets font metrics based on the given context and default font size
596+
* @param context
597+
* @param defaultFontSize
598+
* @return FontMetrics object or null if context or display metrics is null
599+
*/
600+
public static Paint.FontMetrics getFontMetrics(Context context, double defaultFontSize) {
601+
if (context != null && context.getResources().getDisplayMetrics() != null) {
602+
return new Paint() {{
603+
setTextSize(TypedValue.applyDimension(
604+
TypedValue.COMPLEX_UNIT_SP,
605+
(float) defaultFontSize,
606+
context.getResources().getDisplayMetrics()
607+
));
608+
}}.getFontMetrics();
609+
} else {
610+
return null;
611+
}
612+
}
613+
594614
private static class ColorStates
595615
{
596616
static final int[] EMPTY = new int[] { };

0 commit comments

Comments
 (0)