Skip to content

Commit 045986d

Browse files
CatchABusNathanWalker
authored andcommitted
fix: Corrected font-weight support for span
1 parent b12c415 commit 045986d

File tree

6 files changed

+16
-35
lines changed

6 files changed

+16
-35
lines changed
-66 Bytes
Binary file not shown.

packages/core/ui/styling/font.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Font extends FontBase {
4747
return this._typeface;
4848
}
4949

50-
getUIFont(defaultFont: UIFont): UIFont {
50+
getUIFont(defaultFont: any): any {
5151
return undefined;
5252
}
5353
}
@@ -121,7 +121,7 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
121121
function createTypeface(font: Font): android.graphics.Typeface {
122122
const fontFamilies = parseFontFamily(font.fontFamily);
123123
const fontWeight = font.fontWeight;
124-
const isNumericFontWeightSupported = SDK_VERSION > 27;
124+
const isNumericFontWeightSupported = SDK_VERSION >= 28;
125125

126126
let result: android.graphics.Typeface;
127127
let fontStyle: number = 0;

packages/core/ui/styling/font.ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class Font extends FontBase {
109109
});
110110
}
111111

112-
getAndroidTypeface(): android.graphics.Typeface {
112+
getAndroidTypeface(): any {
113113
return undefined;
114114
}
115115
}

packages/core/ui/text-base/index.android.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getClosestPropertyValue, maxLinesProperty, textOverflowProperty } from
33
import { ShadowCSSValues } from '../styling/css-shadow';
44

55
// Requires
6-
import { Font, isFontWeightBold } from '../styling/font';
6+
import { Font } from '../styling/font';
77
import { backgroundColorProperty } from '../styling/style-properties';
88
import { TextBaseCommon, formattedTextProperty, textAlignmentProperty, textDecorationProperty, textProperty, textTransformProperty, textShadowProperty, textStrokeProperty, letterSpacingProperty, whiteSpaceProperty, lineHeightProperty, resetSymbol } from './text-base-common';
99
import { Color } from '../../color';
@@ -593,33 +593,17 @@ function createSpannableStringBuilder(formattedString: FormattedString, defaultF
593593

594594
function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span, start: number, end: number, defaultFontSize: number): void {
595595
const spanStyle = span.style;
596-
const bold = isFontWeightBold(spanStyle.fontWeight);
597-
const italic = spanStyle.fontStyle === 'italic';
598596
const align = spanStyle.verticalAlignment;
599597

600-
// We set font style using StyleSpan in case the font doesn't support font styles
601-
if (bold && italic) {
602-
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
603-
} else if (bold) {
604-
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
605-
} else if (italic) {
606-
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
607-
}
608-
609-
const fontFamily = span.fontFamily;
610-
if (fontFamily) {
611-
const font = new Font(fontFamily, 0, spanStyle.fontStyle, spanStyle.fontWeight, spanStyle.fontScaleInternal, spanStyle.fontVariationSettings);
612-
const typeface = font.getAndroidTypeface() || android.graphics.Typeface.create(fontFamily, 0);
613-
const typefaceSpan: android.text.style.TypefaceSpan = new org.nativescript.widgets.CustomTypefaceSpan(fontFamily, typeface);
614-
ssb.setSpan(typefaceSpan, start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
615-
}
598+
const font = new Font(spanStyle.fontFamily, spanStyle.fontSize, spanStyle.fontStyle, spanStyle.fontWeight, spanStyle.fontScaleInternal, spanStyle.fontVariationSettings);
599+
const typefaceSpan = new org.nativescript.widgets.CustomTypefaceSpan(font.getAndroidTypeface());
600+
ssb.setSpan(typefaceSpan, start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
616601

617-
const realFontSize = span.fontSize;
618-
if (realFontSize) {
619-
ssb.setSpan(new android.text.style.AbsoluteSizeSpan(realFontSize * layout.getDisplayDensity()), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
602+
if (spanStyle.fontSize) {
603+
ssb.setSpan(new android.text.style.AbsoluteSizeSpan(layout.toDevicePixels(spanStyle.fontSize)), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
620604
}
621605

622-
const color = span.color;
606+
const color = spanStyle.color;
623607
if (color) {
624608
ssb.setSpan(new android.text.style.ForegroundColorSpan(color.android), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
625609
}
@@ -646,7 +630,7 @@ function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span,
646630

647631
if (align) {
648632
initializeBaselineAdjustedSpan();
649-
ssb.setSpan(new BaselineAdjustedSpan(defaultFontSize * layout.getDisplayDensity(), align), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
633+
ssb.setSpan(new BaselineAdjustedSpan(layout.toDevicePixels(defaultFontSize), align), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
650634
}
651635

652636
const tappable = span.tappable;

packages/types-android/src/lib/android/org.nativescript.widgets.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@
654654
}
655655

656656
export class CustomTypefaceSpan extends android.text.style.TypefaceSpan {
657-
constructor(family: string, typeface: android.graphics.Typeface);
657+
constructor(typeface: android.graphics.Typeface);
658658
}
659659
}
660660
}

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
/**
99
* Created by hhristov on 2/27/17.
10+
* Updated by CatchABus on 1/26/25.
1011
*/
11-
1212
@SuppressLint("ParcelCreator")
1313
public class CustomTypefaceSpan extends TypefaceSpan {
14-
private final Typeface typeface;
15-
16-
public CustomTypefaceSpan(String family, Typeface typeface) {
17-
super(family);
18-
this.typeface = typeface;
14+
public CustomTypefaceSpan(Typeface typeface) {
15+
super(typeface);
1916
}
2017

2118
public void updateDrawState(TextPaint ds) {
@@ -30,7 +27,7 @@ private void applyCustomTypeFace(TextPaint paint) {
3027
final Typeface old = paint.getTypeface();
3128
final int oldStyle = (old == null) ? 0 : old.getStyle();
3229

33-
Typeface typeface = this.typeface;
30+
Typeface typeface = this.getTypeface();
3431
int fake = oldStyle & ~typeface.getStyle();
3532
if ((fake & android.graphics.Typeface.BOLD) != 0) {
3633
paint.setFakeBoldText(true);

0 commit comments

Comments
 (0)