diff --git a/packages/core/platforms/android/widgets-release.aar b/packages/core/platforms/android/widgets-release.aar index 8846b54c0..65b107f6d 100644 Binary files a/packages/core/platforms/android/widgets-release.aar and b/packages/core/platforms/android/widgets-release.aar differ diff --git a/packages/core/ui/styling/font.android.ts b/packages/core/ui/styling/font.android.ts index 81a628bc5..32ba83981 100644 --- a/packages/core/ui/styling/font.android.ts +++ b/packages/core/ui/styling/font.android.ts @@ -47,7 +47,7 @@ export class Font extends FontBase { return this._typeface; } - getUIFont(defaultFont: UIFont): UIFont { + getUIFont(defaultFont: any): any { return undefined; } } @@ -121,7 +121,7 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type function createTypeface(font: Font): android.graphics.Typeface { const fontFamilies = parseFontFamily(font.fontFamily); const fontWeight = font.fontWeight; - const isNumericFontWeightSupported = SDK_VERSION > 27; + const isNumericFontWeightSupported = SDK_VERSION >= 28; let result: android.graphics.Typeface; let fontStyle: number = 0; diff --git a/packages/core/ui/styling/font.ios.ts b/packages/core/ui/styling/font.ios.ts index 10f807da2..e29ac3def 100644 --- a/packages/core/ui/styling/font.ios.ts +++ b/packages/core/ui/styling/font.ios.ts @@ -109,7 +109,7 @@ export class Font extends FontBase { }); } - getAndroidTypeface(): android.graphics.Typeface { + getAndroidTypeface(): any { return undefined; } } diff --git a/packages/core/ui/text-base/index.android.ts b/packages/core/ui/text-base/index.android.ts index 99c3e665c..9136abfe3 100644 --- a/packages/core/ui/text-base/index.android.ts +++ b/packages/core/ui/text-base/index.android.ts @@ -3,7 +3,7 @@ import { getClosestPropertyValue, maxLinesProperty, textOverflowProperty } from import { ShadowCSSValues } from '../styling/css-shadow'; // Requires -import { Font, isFontWeightBold } from '../styling/font'; +import { Font } from '../styling/font'; import { backgroundColorProperty } from '../styling/style-properties'; import { TextBaseCommon, formattedTextProperty, textAlignmentProperty, textDecorationProperty, textProperty, textTransformProperty, textShadowProperty, textStrokeProperty, letterSpacingProperty, whiteSpaceProperty, lineHeightProperty, resetSymbol } from './text-base-common'; import { Color } from '../../color'; @@ -593,33 +593,17 @@ function createSpannableStringBuilder(formattedString: FormattedString, defaultF function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span, start: number, end: number, defaultFontSize: number): void { const spanStyle = span.style; - const bold = isFontWeightBold(spanStyle.fontWeight); - const italic = spanStyle.fontStyle === 'italic'; const align = spanStyle.verticalAlignment; - // We set font style using StyleSpan in case the font doesn't support font styles - if (bold && italic) { - ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else if (bold) { - ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } else if (italic) { - ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + const font = new Font(spanStyle.fontFamily, spanStyle.fontSize, spanStyle.fontStyle, spanStyle.fontWeight, spanStyle.fontScaleInternal, spanStyle.fontVariationSettings); + const typefaceSpan = new org.nativescript.widgets.CustomTypefaceSpan(font.getAndroidTypeface()); + ssb.setSpan(typefaceSpan, start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + + if (spanStyle.fontSize) { + ssb.setSpan(new android.text.style.AbsoluteSizeSpan(layout.toDevicePixels(spanStyle.fontSize)), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } - const fontFamily = span.fontFamily; - if (fontFamily) { - const font = new Font(fontFamily, 0, spanStyle.fontStyle, spanStyle.fontWeight, spanStyle.fontScaleInternal, spanStyle.fontVariationSettings); - const typeface = font.getAndroidTypeface() || android.graphics.Typeface.create(fontFamily, 0); - const typefaceSpan: android.text.style.TypefaceSpan = new org.nativescript.widgets.CustomTypefaceSpan(fontFamily, typeface); - ssb.setSpan(typefaceSpan, start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } - - const realFontSize = span.fontSize; - if (realFontSize) { - ssb.setSpan(new android.text.style.AbsoluteSizeSpan(realFontSize * layout.getDisplayDensity()), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - } - - const color = span.color; + const color = spanStyle.color; if (color) { ssb.setSpan(new android.text.style.ForegroundColorSpan(color.android), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -646,7 +630,7 @@ function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span, if (align) { initializeBaselineAdjustedSpan(); - ssb.setSpan(new BaselineAdjustedSpan(defaultFontSize * layout.getDisplayDensity(), align), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + ssb.setSpan(new BaselineAdjustedSpan(layout.toDevicePixels(defaultFontSize), align), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } const tappable = span.tappable; diff --git a/packages/types-android/src/lib/android/org.nativescript.widgets.d.ts b/packages/types-android/src/lib/android/org.nativescript.widgets.d.ts index 125841d6d..9a1a5eb62 100644 --- a/packages/types-android/src/lib/android/org.nativescript.widgets.d.ts +++ b/packages/types-android/src/lib/android/org.nativescript.widgets.d.ts @@ -654,7 +654,7 @@ } export class CustomTypefaceSpan extends android.text.style.TypefaceSpan { - constructor(family: string, typeface: android.graphics.Typeface); + constructor(typeface: android.graphics.Typeface); } } } diff --git a/packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java b/packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java index ce6255759..fd0d26583 100644 --- a/packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java +++ b/packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java @@ -7,15 +7,12 @@ import android.text.style.TypefaceSpan; /** * Created by hhristov on 2/27/17. + * Updated by CatchABus on 1/26/25. */ - @SuppressLint("ParcelCreator") public class CustomTypefaceSpan extends TypefaceSpan { - private final Typeface typeface; - - public CustomTypefaceSpan(String family, Typeface typeface) { - super(family); - this.typeface = typeface; + public CustomTypefaceSpan(Typeface typeface) { + super(typeface); } public void updateDrawState(TextPaint ds) { @@ -30,7 +27,7 @@ public class CustomTypefaceSpan extends TypefaceSpan { final Typeface old = paint.getTypeface(); final int oldStyle = (old == null) ? 0 : old.getStyle(); - Typeface typeface = this.typeface; + Typeface typeface = this.getTypeface(); int fake = oldStyle & ~typeface.getStyle(); if ((fake & android.graphics.Typeface.BOLD) != 0) { paint.setFakeBoldText(true);