fix(core): font-weight allow passing number (#10072)

This commit is contained in:
farfromrefuge
2022-11-07 21:52:13 +00:00
committed by GitHub
parent 6934645423
commit 5f3f1ace28
5 changed files with 13 additions and 7 deletions

View File

@ -27,8 +27,8 @@ export abstract class Font implements FontDefinition {
public abstract getAndroidTypeface(): any; /* android.graphics.Typeface */ public abstract getAndroidTypeface(): any; /* android.graphics.Typeface */
public abstract getUIFont(defaultFont: any /* UIFont */): any; /* UIFont */ public abstract getUIFont(defaultFont: any /* UIFont */): any; /* UIFont */
public abstract withFontFamily(family: string): Font; public abstract withFontFamily(family: string): Font;
public abstract withFontStyle(style: string): Font; public abstract withFontStyle(style: FontStyleType): Font;
public abstract withFontWeight(weight: string): Font; public abstract withFontWeight(weight: FontWeightType): Font;
public abstract withFontSize(size: number): Font; public abstract withFontSize(size: number): Font;
public abstract withFontScale(scale: number): Font; public abstract withFontScale(scale: number): Font;
@ -55,7 +55,7 @@ export namespace FontStyle {
export const parse = makeParser<FontStyleType>(isValid); export const parse = makeParser<FontStyleType>(isValid);
} }
export type FontWeightType = '100' | '200' | '300' | 'normal' | '400' | '500' | '600' | 'bold' | '700' | '800' | '900'; export type FontWeightType = '100' | '200' | '300' | 'normal' | '400' | '500' | '600' | 'bold' | '700' | '800' | '900' | number;
export namespace FontWeight { export namespace FontWeight {
export const THIN = '100'; export const THIN = '100';
export const EXTRA_LIGHT = '200'; export const EXTRA_LIGHT = '200';

View File

@ -1,6 +1,6 @@
export type FontStyle = 'normal' | 'italic'; export type FontStyle = 'normal' | 'italic';
export type FontWeight = '100' | '200' | '300' | 'normal' | '400' | '500' | '600' | 'bold' | '700' | '800' | '900'; export type FontWeight = '100' | '200' | '300' | 'normal' | '400' | '500' | '600' | 'bold' | '700' | '800' | '900' | number;
export interface ParsedFont { export interface ParsedFont {
fontStyle?: FontStyle; fontStyle?: FontStyle;

View File

@ -139,6 +139,9 @@ function createTypeface(font: Font): android.graphics.Typeface {
} }
function getFontWeightSuffix(fontWeight: FontWeightType): string { function getFontWeightSuffix(fontWeight: FontWeightType): string {
if (typeof fontWeight === 'number') {
fontWeight = (fontWeight + '') as any;
}
switch (fontWeight) { switch (fontWeight) {
case FontWeight.THIN: case FontWeight.THIN:
return android.os.Build.VERSION.SDK_INT >= 16 ? '-thin' : ''; return android.os.Build.VERSION.SDK_INT >= 16 ? '-thin' : '';

View File

@ -16,8 +16,8 @@
public getUIFont(defaultFont: any /* UIFont */): any /* UIFont */; public getUIFont(defaultFont: any /* UIFont */): any /* UIFont */;
public withFontFamily(family: string): Font; public withFontFamily(family: string): Font;
public withFontStyle(style: string): Font; public withFontStyle(style: FontStyle): Font;
public withFontWeight(weight: string): Font; public withFontWeight(weight: FontWeight): Font;
public withFontSize(size: number): Font; public withFontSize(size: number): Font;
public withFontScale(scale: number): Font; public withFontScale(scale: number): Font;
@ -32,7 +32,7 @@ export namespace FontStyle {
export function parse(value: string): FontStyle; export function parse(value: string): FontStyle;
} }
export type FontWeight = '100' | '200' | '300' | 'normal' | '400' | '500' | '600' | 'bold' | '700' | '800' | '900'; export type FontWeight = '100' | '200' | '300' | 'normal' | '400' | '500' | '600' | 'bold' | '700' | '800' | '900' | number;
export namespace FontWeight { export namespace FontWeight {
export const THIN: '100'; export const THIN: '100';
export const EXTRA_LIGHT: '200'; export const EXTRA_LIGHT: '200';

View File

@ -80,6 +80,9 @@ export class Font extends FontBase {
} }
function getNativeFontWeight(fontWeight: FontWeightType): number { function getNativeFontWeight(fontWeight: FontWeightType): number {
if (typeof fontWeight === 'number') {
fontWeight = (fontWeight + '') as any;
}
switch (fontWeight) { switch (fontWeight) {
case FontWeight.THIN: case FontWeight.THIN:
return UIFontWeightUltraLight; return UIFontWeightUltraLight;