mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Add support for CSS font-weight + sample app for testing fonts
Resolves #1862
This commit is contained in:
39
ui/enums/enums.d.ts
vendored
39
ui/enums/enums.d.ts
vendored
@@ -485,14 +485,49 @@
|
||||
*/
|
||||
export module FontWeight {
|
||||
/**
|
||||
* Normal font weight.
|
||||
* Thin font weight. CSS font-weight 100.
|
||||
*/
|
||||
export var thin: string;
|
||||
|
||||
/**
|
||||
* Extra-light / Ultra-light font weight. CSS font-weight 200.
|
||||
*/
|
||||
export var extraLight: string;
|
||||
|
||||
/**
|
||||
* Light font weight. CSS font-weight 300.
|
||||
*/
|
||||
export var light: string;
|
||||
|
||||
/**
|
||||
* Normal font weight. CSS font-weight 400.
|
||||
*/
|
||||
export var normal: string;
|
||||
|
||||
/**
|
||||
* Bold font weight.
|
||||
* Medium font weight. CSS font-weight 500.
|
||||
*/
|
||||
export var medium: string;
|
||||
|
||||
/**
|
||||
* Semi-bold / Demi-bold font weight. CSS font-weight 600.
|
||||
*/
|
||||
export var semiBold: string;
|
||||
|
||||
/**
|
||||
* Bold font weight. CSS font-weight 700.
|
||||
*/
|
||||
export var bold: string;
|
||||
|
||||
/**
|
||||
* Extra-bold / Ultra-bold font weight. CSS font-weight 800.
|
||||
*/
|
||||
export var extraBold: string;
|
||||
|
||||
/**
|
||||
* Black font weight. CSS font-weight 900.
|
||||
*/
|
||||
export var black: string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -146,8 +146,15 @@ export module FontStyle {
|
||||
}
|
||||
|
||||
export module FontWeight {
|
||||
export var normal: string = "normal";
|
||||
export var bold: string = "bold";
|
||||
export var thin: string = "100";
|
||||
export var extraLight: string = "200";
|
||||
export var light: string = "300";
|
||||
export var normal: string = "normal"; // 400
|
||||
export var medium: string = "500";
|
||||
export var semiBold: string = "600";
|
||||
export var bold: string = "bold"; // 700
|
||||
export var extraBold: string = "800";
|
||||
export var black: string = "900";
|
||||
}
|
||||
|
||||
export module BackgroundRepeat {
|
||||
|
||||
@@ -39,14 +39,15 @@ export class Font implements definitios.Font {
|
||||
}
|
||||
|
||||
get isBold(): boolean {
|
||||
return this._fontWeight.toLowerCase() === enums.FontWeight.bold;;
|
||||
return this._fontWeight.toLowerCase() === enums.FontWeight.bold
|
||||
|| this._fontWeight.toLowerCase() === "700";
|
||||
}
|
||||
set isBold(value: boolean) {
|
||||
throw new Error("isBold is read-only");
|
||||
}
|
||||
|
||||
get isItalic(): boolean {
|
||||
return this._fontStyle.toLowerCase() === enums.FontStyle.italic;;
|
||||
return this._fontStyle.toLowerCase() === enums.FontStyle.italic;
|
||||
}
|
||||
set isItalic(value: boolean) {
|
||||
throw new Error("isItalic is read-only");
|
||||
@@ -135,8 +136,21 @@ export module genericFontFamilies {
|
||||
|
||||
var styles = new Set();
|
||||
["italic", "oblique"].forEach((val, i, a) => styles.add(val));
|
||||
|
||||
// http://www.w3schools.com/cssref/pr_font_weight.asp
|
||||
//- normal(same as 400)
|
||||
//- bold(same as 700)
|
||||
//- 100(Thin) (API16 -thin)
|
||||
//- 200(Extra Light / Ultra Light) (API16 -light)
|
||||
//- 300(Light) (API16 -light)
|
||||
//- 400(Normal)
|
||||
//- 500(Medium) (API21 -medium)
|
||||
//- 600(Semi Bold / Demi Bold) (API21 -medium)
|
||||
//- 700(Bold) (API16 -bold)
|
||||
//- 800(Extra Bold / Ultra Bold) (API16 -bold)
|
||||
//- 900(Black / Heavy) (API21 -black)
|
||||
var weights = new Set();
|
||||
["bold", "bolder", "lighter", "100", "200", "300", "400", "500", "600", "700", "800", "900"].forEach((val, i, a) => weights.add(val));
|
||||
["normal", "bold", "100", "200", "300", "400", "500", "600", "700", "800", "900"].forEach((val, i, a) => weights.add(val));
|
||||
|
||||
interface ParsedFont {
|
||||
fontStyle?: string;
|
||||
|
||||
@@ -64,102 +64,123 @@ export class Font extends common.Font {
|
||||
|
||||
public getAndroidTypeface(): android.graphics.Typeface {
|
||||
if (!this._typeface) {
|
||||
var style: number = 0;
|
||||
|
||||
if (this.isBold) {
|
||||
style |= android.graphics.Typeface.BOLD;
|
||||
}
|
||||
|
||||
if (this.isItalic) {
|
||||
style |= android.graphics.Typeface.ITALIC;
|
||||
}
|
||||
|
||||
var typeFace = this.getTypeFace(this.fontFamily);
|
||||
this._typeface = android.graphics.Typeface.create(typeFace, style);
|
||||
this._typeface = createTypeface(this);
|
||||
}
|
||||
|
||||
return this._typeface;
|
||||
}
|
||||
}
|
||||
|
||||
private getTypeFace(fontFamily: string): android.graphics.Typeface {
|
||||
var fonts = common.parseFontFamily(fontFamily);
|
||||
var result = null;
|
||||
if (fonts.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (var i = 0; i < fonts.length; i++) {
|
||||
switch (fonts[i].toLowerCase()) {
|
||||
case common.genericFontFamilies.serif:
|
||||
result = android.graphics.Typeface.SERIF;
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.sansSerif:
|
||||
result = android.graphics.Typeface.SANS_SERIF;
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.monospace:
|
||||
result = android.graphics.Typeface.MONOSPACE;
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.system:
|
||||
result = android.graphics.Typeface.DEFAULT;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = this.loadFontFromFile(fonts[i]);
|
||||
break;
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
function loadFontFromFile(fontFamily: string): android.graphics.Typeface {
|
||||
ensureApplication();
|
||||
|
||||
appAssets = appAssets || application.android.context.getAssets();
|
||||
if (!appAssets) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private loadFontFromFile(fontFamily: string): android.graphics.Typeface {
|
||||
ensureApplication();
|
||||
ensureTypes();
|
||||
|
||||
appAssets = appAssets || application.android.context.getAssets();
|
||||
if (!appAssets) {
|
||||
return null;
|
||||
var result = typefaceCache.get(fontFamily);
|
||||
// Check for undefined explicitly as null mean we tried to load the font, but failed.
|
||||
if (types.isUndefined(result)) {
|
||||
result = null;
|
||||
|
||||
ensureTrace();
|
||||
ensureFS();
|
||||
|
||||
var fontAssetPath: string;
|
||||
var basePath = fs.path.join(fs.knownFolders.currentApp().path, "fonts", fontFamily);
|
||||
if (fs.File.exists(basePath + ".ttf")) {
|
||||
fontAssetPath = FONTS_BASE_PATH + fontFamily + ".ttf";
|
||||
}
|
||||
else if (fs.File.exists(basePath + ".otf")) {
|
||||
fontAssetPath = FONTS_BASE_PATH + fontFamily + ".otf";
|
||||
}
|
||||
else {
|
||||
trace.write("Could not find font file for " + fontFamily, trace.categories.Error, trace.messageType.error);
|
||||
}
|
||||
|
||||
ensureTypes();
|
||||
|
||||
var result = typefaceCache.get(fontFamily);
|
||||
// Check for undefined explicitly as null mean we tried to load the font, but failed.
|
||||
if (types.isUndefined(result)) {
|
||||
result = null;
|
||||
|
||||
ensureTrace();
|
||||
ensureFS();
|
||||
|
||||
var fontAssetPath: string;
|
||||
var basePath = fs.path.join(fs.knownFolders.currentApp().path, "fonts", fontFamily);
|
||||
if (fs.File.exists(basePath + ".ttf")) {
|
||||
fontAssetPath = FONTS_BASE_PATH + fontFamily + ".ttf";
|
||||
}
|
||||
else if (fs.File.exists(basePath + ".otf")) {
|
||||
fontAssetPath = FONTS_BASE_PATH + fontFamily + ".otf";
|
||||
}
|
||||
else {
|
||||
trace.write("Could not find font file for " + fontFamily, trace.categories.Error, trace.messageType.error);
|
||||
if (fontAssetPath) {
|
||||
try {
|
||||
fontAssetPath = fs.path.join(fs.knownFolders.currentApp().path, fontAssetPath);
|
||||
result = android.graphics.Typeface.createFromFile(fontAssetPath)
|
||||
} catch (e) {
|
||||
trace.write("Error loading font asset: " + fontAssetPath, trace.categories.Error, trace.messageType.error);
|
||||
}
|
||||
}
|
||||
typefaceCache.set(fontFamily, result);
|
||||
}
|
||||
|
||||
if (fontAssetPath) {
|
||||
try {
|
||||
fontAssetPath = fs.path.join(fs.knownFolders.currentApp().path, fontAssetPath);
|
||||
result = android.graphics.Typeface.createFromFile(fontAssetPath)
|
||||
} catch (e) {
|
||||
trace.write("Error loading font asset: " + fontAssetPath, trace.categories.Error, trace.messageType.error);
|
||||
}
|
||||
}
|
||||
typefaceCache.set(fontFamily, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
function createTypeface(font: Font): android.graphics.Typeface {
|
||||
//http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
|
||||
|
||||
var fontStyle = 0;
|
||||
if (font.isBold) {
|
||||
fontStyle |= android.graphics.Typeface.BOLD;
|
||||
}
|
||||
if (font.isItalic) {
|
||||
fontStyle |= android.graphics.Typeface.ITALIC;
|
||||
}
|
||||
|
||||
var fonts = common.parseFontFamily(font.fontFamily);
|
||||
var result = null;
|
||||
if (fonts.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (var i = 0; i < fonts.length; i++) {
|
||||
switch (fonts[i].toLowerCase()) {
|
||||
case common.genericFontFamilies.serif:
|
||||
result = android.graphics.Typeface.create("serif" + getFontWeightSuffix(font.fontWeight), fontStyle);
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.sansSerif:
|
||||
case common.genericFontFamilies.system:
|
||||
result = android.graphics.Typeface.create("sans-serif" + getFontWeightSuffix(font.fontWeight), fontStyle);
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.monospace:
|
||||
result = android.graphics.Typeface.create("monospace" + getFontWeightSuffix(font.fontWeight), fontStyle);
|
||||
break;
|
||||
|
||||
default:
|
||||
result = loadFontFromFile(fonts[i]);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getFontWeightSuffix(fontWeight: string): string {
|
||||
switch (fontWeight) {
|
||||
case enums.FontWeight.thin:
|
||||
return android.os.Build.VERSION.SDK_INT >= 16 ? "-thin" : "";
|
||||
case enums.FontWeight.extraLight:
|
||||
case enums.FontWeight.light:
|
||||
return android.os.Build.VERSION.SDK_INT >= 16 ? "-light" : "";
|
||||
case enums.FontWeight.normal:
|
||||
case "400":
|
||||
case undefined:
|
||||
case null:
|
||||
return "";
|
||||
case enums.FontWeight.medium:
|
||||
case enums.FontWeight.semiBold:
|
||||
return android.os.Build.VERSION.SDK_INT >= 21 ? "-medium" : "";
|
||||
case enums.FontWeight.bold:
|
||||
case "700":
|
||||
case enums.FontWeight.extraBold:
|
||||
return "";
|
||||
case enums.FontWeight.black:
|
||||
return android.os.Build.VERSION.SDK_INT >= 21 ? "-black" : "";
|
||||
default:
|
||||
throw new Error(`Invalid font weight: "${fontWeight}"`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,42 +14,7 @@ export class Font extends common.Font {
|
||||
|
||||
public getUIFont(defaultFont: UIFont): UIFont {
|
||||
if (!this._uiFont) {
|
||||
var size = this.fontSize || defaultFont.pointSize;
|
||||
|
||||
var symbolicTraits: number = 0;
|
||||
if (this.isBold) {
|
||||
symbolicTraits |= UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitBold;
|
||||
}
|
||||
if (this.isItalic) {
|
||||
symbolicTraits |= UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitItalic;
|
||||
}
|
||||
|
||||
var descriptor: UIFontDescriptor;
|
||||
switch (this.fontFamily) {
|
||||
|
||||
case common.genericFontFamilies.sansSerif:
|
||||
case common.genericFontFamilies.system:
|
||||
let uiFont = UIFont.systemFontOfSize(size);
|
||||
descriptor = uiFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.monospace:
|
||||
if ((<any>UIFont).monospacedDigitSystemFontOfSizeWeight) {// This method is available on iOS 9.0 and later.
|
||||
let uiFont = (<any>UIFont).monospacedDigitSystemFontOfSizeWeight(size, 0);
|
||||
descriptor = uiFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!descriptor) {
|
||||
descriptor = resolveFontDescriptor(this.fontFamily, symbolicTraits);
|
||||
}
|
||||
|
||||
if (!descriptor) {
|
||||
descriptor = defaultFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
}
|
||||
|
||||
this._uiFont = UIFont.fontWithDescriptorSize(descriptor, size);
|
||||
this._uiFont = createUIFont(this, defaultFont);
|
||||
}
|
||||
return this._uiFont;
|
||||
}
|
||||
@@ -72,70 +37,26 @@ export class Font extends common.Font {
|
||||
}
|
||||
|
||||
var areSystemFontSetsValid: boolean = false;
|
||||
var systemFontFamilies = new Set();
|
||||
var systemFonts = new Set();
|
||||
export var systemFontFamilies = new Set<string>();
|
||||
export var systemFonts = new Set<string>();
|
||||
|
||||
function assureSystemFontSets() {
|
||||
if (!areSystemFontSetsValid) {
|
||||
var nsFontFamilies = UIFont.familyNames();
|
||||
for (var i = 0; i < nsFontFamilies.count; i++) {
|
||||
var family = nsFontFamilies.objectAtIndex(i);
|
||||
systemFontFamilies.add(family);
|
||||
|
||||
var nsFonts = UIFont.fontNamesForFamilyName(family);
|
||||
for (var j = 0; j < nsFonts.count; j++) {
|
||||
var font = nsFonts.objectAtIndex(j);
|
||||
systemFonts.add(font);
|
||||
}
|
||||
}
|
||||
areSystemFontSetsValid = true;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveFontDescriptor(fontFamilyValue: string, symbolicTraits: number): UIFontDescriptor {
|
||||
var fonts = common.parseFontFamily(fontFamilyValue);
|
||||
var result: UIFontDescriptor = null;
|
||||
if (fonts.length === 0) {
|
||||
return null;
|
||||
export function ensureSystemFontSets() {
|
||||
if (areSystemFontSetsValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
assureSystemFontSets();
|
||||
var nsFontFamilies = UIFont.familyNames();
|
||||
for (var i = 0; i < nsFontFamilies.count; i++) {
|
||||
var family = nsFontFamilies.objectAtIndex(i);
|
||||
systemFontFamilies.add(family);
|
||||
|
||||
for (var i = 0; i < fonts.length; i++) {
|
||||
var fontFamily = getFontFamilyRespectingGenericFonts(fonts[i]);
|
||||
|
||||
if (systemFonts.has(fontFamily)) {
|
||||
// This is an actual font - don't apply symbolic traits
|
||||
result = UIFontDescriptor.fontDescriptorWithNameSize(fontFamily, 0);
|
||||
} else if (systemFontFamilies.has(fontFamily)) {
|
||||
// This is a font family - we should apply symbolic traits if there are such
|
||||
var fontFaceAttributes = [];
|
||||
|
||||
if (!symbolicTraits) {
|
||||
fontFaceAttributes.push("Regular");
|
||||
}
|
||||
else {
|
||||
if (symbolicTraits & UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitBold) {
|
||||
fontFaceAttributes.push("Bold");
|
||||
}
|
||||
if (symbolicTraits & UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitItalic) {
|
||||
fontFaceAttributes.push("Italic");
|
||||
}
|
||||
}
|
||||
|
||||
var fontAttributes = NSMutableDictionary.alloc().init();
|
||||
fontAttributes.setObjectForKey(fontFamily, "NSFontFamilyAttribute");
|
||||
fontAttributes.setObjectForKey(fontFaceAttributes.join(" "), "NSFontFaceAttribute");
|
||||
|
||||
result = UIFontDescriptor.fontDescriptorWithFontAttributes(fontAttributes);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
var nsFonts = UIFont.fontNamesForFamilyName(family);
|
||||
for (var j = 0; j < nsFonts.count; j++) {
|
||||
var font = nsFonts.objectAtIndex(j);
|
||||
systemFonts.add(font);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
areSystemFontSetsValid = true;
|
||||
}
|
||||
|
||||
const DEFAULT_SERIF = "Times New Roman";
|
||||
@@ -158,6 +79,212 @@ function getFontFamilyRespectingGenericFonts(fontFamily: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
function createUIFont(font: Font, defaultFont: UIFont) {
|
||||
var size = font.fontSize || defaultFont.pointSize;
|
||||
var descriptor: UIFontDescriptor;
|
||||
|
||||
var symbolicTraits: number = 0;
|
||||
if (font.isBold) {
|
||||
symbolicTraits |= UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitBold;
|
||||
}
|
||||
if (font.isItalic) {
|
||||
symbolicTraits |= UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitItalic;
|
||||
}
|
||||
|
||||
descriptor = tryResolveWithSystemFont(font, size, symbolicTraits);
|
||||
|
||||
if (!descriptor) {
|
||||
descriptor = tryResolveByFamily(font);
|
||||
}
|
||||
|
||||
if (!descriptor) {
|
||||
descriptor = defaultFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
}
|
||||
|
||||
return UIFont.fontWithDescriptorSize(descriptor, size);
|
||||
}
|
||||
|
||||
function tryResolveWithSystemFont(font: Font, size: number, symbolicTraits: number): UIFontDescriptor {
|
||||
var systemFont: UIFont;
|
||||
switch (font.fontFamily) {
|
||||
case common.genericFontFamilies.sansSerif:
|
||||
case common.genericFontFamilies.system:
|
||||
if ((<any>UIFont).systemFontOfSizeWeight) {// This method is available on iOS 8.2 and later.
|
||||
systemFont = (<any>UIFont).systemFontOfSizeWeight(size, getiOSFontWeight(font.fontWeight));
|
||||
}
|
||||
else {
|
||||
systemFont = UIFont.systemFontOfSize(size);
|
||||
}
|
||||
result = systemFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
break;
|
||||
|
||||
case common.genericFontFamilies.monospace:
|
||||
if ((<any>UIFont).monospacedDigitSystemFontOfSizeWeight) {// This method is available on iOS 9.0 and later.
|
||||
systemFont = (<any>UIFont).monospacedDigitSystemFontOfSizeWeight(size, getiOSFontWeight(font.fontWeight));
|
||||
result = systemFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (systemFont) {
|
||||
var result = systemFont.fontDescriptor().fontDescriptorWithSymbolicTraits(symbolicTraits);
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function tryResolveByFamily(font: Font): UIFontDescriptor {
|
||||
var fonts = common.parseFontFamily(font.fontFamily);
|
||||
var result: UIFontDescriptor = null;
|
||||
if (fonts.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ensureSystemFontSets();
|
||||
|
||||
for (var i = 0; i < fonts.length; i++) {
|
||||
var fontFamily = getFontFamilyRespectingGenericFonts(fonts[i]);
|
||||
var fontFace = getiOSFontFace(fontFamily, font.fontWeight, font.isItalic);
|
||||
if (systemFonts.has(fontFamily) && !fontFace) { // This is an actual font like `HelveticaNeue-UltraLightItalic` - don't apply font face attribute
|
||||
result = UIFontDescriptor.fontDescriptorWithNameSize(fontFamily, 0);
|
||||
}
|
||||
else if (systemFontFamilies.has(fontFamily)) { // This is a font family like `Helvetica Neue` - apply font face attribute
|
||||
result = createFontDescriptor(fontFamily, fontFace);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function createFontDescriptor(fontFamily: string, fontFace: string): UIFontDescriptor {
|
||||
var fontAttributes = NSMutableDictionary.alloc().init();
|
||||
fontAttributes.setObjectForKey(fontFamily, "NSFontFamilyAttribute");
|
||||
if (fontFace) {
|
||||
fontAttributes.setObjectForKey(fontFace, "NSFontFaceAttribute");
|
||||
}
|
||||
return UIFontDescriptor.fontDescriptorWithFontAttributes(fontAttributes);
|
||||
}
|
||||
|
||||
// Available in iOS 8.2 and later.
|
||||
declare var UIFontWeightThin: number; //0.8
|
||||
declare var UIFontWeightUltraLight: number; //0.6
|
||||
declare var UIFontWeightLight: number; //0.4
|
||||
declare var UIFontWeightRegular: number; //0
|
||||
declare var UIFontWeightMedium: number; //0.23
|
||||
declare var UIFontWeightSemibold: number; //0.3
|
||||
declare var UIFontWeightBold: number; //0.4
|
||||
declare var UIFontWeightHeavy: number; //0.56
|
||||
declare var UIFontWeightBlack: number; //0.62
|
||||
function getiOSFontWeight(fontWeight: string): number {
|
||||
if (!(<any>UIFont).systemFontOfSizeWeight) {
|
||||
throw new Error("Font weight is available in iOS 8.2 and later.");
|
||||
}
|
||||
|
||||
switch (fontWeight) {
|
||||
case enums.FontWeight.thin:
|
||||
return UIFontWeightThin;
|
||||
case enums.FontWeight.extraLight:
|
||||
return UIFontWeightUltraLight;
|
||||
case enums.FontWeight.light:
|
||||
return UIFontWeightLight;
|
||||
case enums.FontWeight.normal:
|
||||
case "400":
|
||||
case undefined:
|
||||
case null:
|
||||
return UIFontWeightRegular;
|
||||
case enums.FontWeight.medium:
|
||||
return UIFontWeightMedium;
|
||||
case enums.FontWeight.semiBold:
|
||||
return UIFontWeightSemibold;
|
||||
case enums.FontWeight.bold:
|
||||
case "700":
|
||||
return UIFontWeightBold;
|
||||
case enums.FontWeight.extraBold:
|
||||
return UIFontWeightHeavy;
|
||||
case enums.FontWeight.black:
|
||||
return UIFontWeightBlack;
|
||||
default:
|
||||
throw new Error(`Invalid font weight: "${fontWeight}"`);
|
||||
}
|
||||
}
|
||||
|
||||
function combineWeightStringWithItalic(weight: string, isItalic: boolean) {
|
||||
if (!isItalic) {
|
||||
return weight;
|
||||
}
|
||||
|
||||
if (!weight) {
|
||||
return "Italic";
|
||||
}
|
||||
|
||||
return weight + " Italic";
|
||||
}
|
||||
|
||||
function canLoadFont(fontFamily: string, fontFace: string) {
|
||||
var trialDescriptor = createFontDescriptor(fontFamily, fontFace);
|
||||
var trialFont = UIFont.fontWithDescriptorSize(trialDescriptor, 0);
|
||||
return trialFont.familyName === fontFamily;
|
||||
}
|
||||
|
||||
function findCorrectWeightString(fontFamily: string, weightStringAlternatives: Array<string>, isItalic: boolean) {
|
||||
var i = 0;
|
||||
let length = weightStringAlternatives.length;
|
||||
for (; i < length; i++) {
|
||||
var possibleFontFace = combineWeightStringWithItalic(weightStringAlternatives[i], isItalic);
|
||||
if (canLoadFont(fontFamily, possibleFontFace)) {
|
||||
return weightStringAlternatives[i];
|
||||
}
|
||||
}
|
||||
return weightStringAlternatives[0];
|
||||
}
|
||||
|
||||
function getiOSFontFace(fontFamily: string, fontWeight: string, isItalic: boolean): string {
|
||||
// ... with a lot of fuzzy logic and artificial intelligence thanks to the lack of font naming standards.
|
||||
var weight: string;
|
||||
switch (fontWeight) {
|
||||
case enums.FontWeight.thin:
|
||||
weight = "Thin";
|
||||
break;
|
||||
case enums.FontWeight.extraLight:
|
||||
weight = findCorrectWeightString(fontFamily, ["Ultra Light", "UltraLight", "Extra Light", "ExtraLight", "Ultra light", "Ultralight", "Extra light", "Extralight"], isItalic);
|
||||
break;
|
||||
case enums.FontWeight.light:
|
||||
weight = "Light";
|
||||
break;
|
||||
case enums.FontWeight.normal:
|
||||
case "400":
|
||||
case undefined:
|
||||
case null:
|
||||
weight = ""; // We dont' need to write Regular
|
||||
break;
|
||||
case enums.FontWeight.medium:
|
||||
weight = "Medium";
|
||||
break;
|
||||
case enums.FontWeight.semiBold:
|
||||
weight = findCorrectWeightString(fontFamily, ["Demi Bold", "DemiBold", "Semi Bold", "SemiBold", "Demi bold", "Demibold", "Semi bold", "Semibold"], isItalic);
|
||||
break;
|
||||
case enums.FontWeight.bold:
|
||||
case "700":
|
||||
weight = "Bold";
|
||||
break;
|
||||
case enums.FontWeight.extraBold:
|
||||
weight = findCorrectWeightString(fontFamily, ["Heavy", "Extra Bold", "ExtraBold", "Extra bold", "Extrabold"], isItalic);
|
||||
break;
|
||||
case enums.FontWeight.black:
|
||||
weight = "Black";
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Invalid font weight: "${fontWeight}"`);
|
||||
}
|
||||
|
||||
return combineWeightStringWithItalic(weight, isItalic);
|
||||
}
|
||||
|
||||
export module ios {
|
||||
export function registerFont(fontFile: string) {
|
||||
var filePath = fs.path.join(fs.knownFolders.currentApp().path, "fonts", fontFile);
|
||||
|
||||
@@ -423,7 +423,19 @@ function isLetterSpacingValid(value: string): boolean {
|
||||
}
|
||||
|
||||
function isFontWeightValid(value: string): boolean {
|
||||
return value === enums.FontWeight.normal || value === enums.FontWeight.bold;
|
||||
if (!value) {
|
||||
console.trace();
|
||||
}
|
||||
return value === enums.FontWeight.thin
|
||||
|| value === enums.FontWeight.extraLight
|
||||
|| value === enums.FontWeight.light
|
||||
|| value === enums.FontWeight.normal || value === "400"
|
||||
|| value === enums.FontWeight.medium
|
||||
|| value === enums.FontWeight.semiBold
|
||||
|| value === enums.FontWeight.bold || value === "700"
|
||||
|| value === enums.FontWeight.extraBold
|
||||
|| value === enums.FontWeight.black
|
||||
;
|
||||
}
|
||||
|
||||
function isFontStyleValid(value: string): boolean {
|
||||
|
||||
Reference in New Issue
Block a user