diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index 6306a2162..8dbbe83f4 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -1866,6 +1866,10 @@ PreserveNewest + + + + diff --git a/apps/tests/fonts/Roboto-Bold.ttf b/apps/tests/fonts/Roboto-Bold.ttf new file mode 100644 index 000000000..aaf374d2c Binary files /dev/null and b/apps/tests/fonts/Roboto-Bold.ttf differ diff --git a/apps/tests/fonts/Roboto-BoldItalic.ttf b/apps/tests/fonts/Roboto-BoldItalic.ttf new file mode 100644 index 000000000..dcd0f8007 Binary files /dev/null and b/apps/tests/fonts/Roboto-BoldItalic.ttf differ diff --git a/apps/tests/fonts/Roboto-Italic.ttf b/apps/tests/fonts/Roboto-Italic.ttf new file mode 100644 index 000000000..f382c6874 Binary files /dev/null and b/apps/tests/fonts/Roboto-Italic.ttf differ diff --git a/apps/tests/fonts/Roboto-Regular.ttf b/apps/tests/fonts/Roboto-Regular.ttf new file mode 100644 index 000000000..3e6e2e761 Binary files /dev/null and b/apps/tests/fonts/Roboto-Regular.ttf differ diff --git a/apps/tests/ui/style/style-properties-tests.ts b/apps/tests/ui/style/style-properties-tests.ts index a0ce35e5d..0b00df829 100644 --- a/apps/tests/ui/style/style-properties-tests.ts +++ b/apps/tests/ui/style/style-properties-tests.ts @@ -5,6 +5,8 @@ import stackModule = require("ui/layouts/stack-layout"); import page = require("ui/page"); import color = require("color"); import observable = require("data/observable"); +import enums = require("ui/enums"); +import fontModule = require("ui/styling/font"); var testBtn: buttonModule.Button; var testPage: page.Page; @@ -317,3 +319,47 @@ function test_font_shorthand_property(short: string, family: string, size: numbe TKUnit.assertEqual(testView.style.fontWeight, weight, "style.fontWeight"); TKUnit.assertEqual(testView.style.fontSize, size, "style.fontSize"); } + +export function test_setting_font_properties_sets_native_font() { + + if (fontModule.ios) { + var basePath = "fonts"; + fontModule.ios.registerFont(basePath + "/Roboto-Regular.ttf"); + fontModule.ios.registerFont(basePath + "/Roboto-Bold.ttf"); + fontModule.ios.registerFont(basePath + "/Roboto-BoldItalic.ttf"); + fontModule.ios.registerFont(basePath + "/Roboto-Italic.ttf"); + } + + test_native_font(enums.FontStyle.normal, enums.FontWeight.normal); + test_native_font(enums.FontStyle.italic, enums.FontWeight.normal); + test_native_font(enums.FontStyle.normal, enums.FontWeight.bold); + test_native_font(enums.FontStyle.italic, enums.FontWeight.bold); +} + +function test_native_font(style: string, weight: string) { + var testView = new buttonModule.Button(); + var fontName = "Roboto"; + var fontNameSuffix = ""; + + testView.style.fontFamily = fontName; + testView.style.fontWeight = weight; + testView.style.fontStyle = style; + + if (style === enums.FontStyle.normal && weight === enums.FontWeight.normal) + { + fontNameSuffix += "Regular"; + } + if (weight === enums.FontWeight.bold) + { + fontNameSuffix += "Bold"; + } + if (style === enums.FontStyle.italic) + { + fontNameSuffix += "Italic"; + } + + if (testView.ios) { + TKUnit.assertEqual((testView.ios).titleLabel.font.fontName.toLowerCase(), (fontName + "-" + fontNameSuffix).toLowerCase(), "native font " + weight + " " + style); + } + //TODO: If needed add tests for other platforms +} \ No newline at end of file diff --git a/ui/styling/font.ios.ts b/ui/styling/font.ios.ts index 26f45bd07..3d334d32e 100644 --- a/ui/styling/font.ios.ts +++ b/ui/styling/font.ios.ts @@ -87,10 +87,25 @@ function resolveFontDescriptor(fontFamilyValue: string, symbolicTraits: number): var fontFamily = getFontFamilyRespectingGenericFonts(fonts[i]); if (systemFontFamilies.has(fontFamily)) { // This is a font family - we should apply symbolic traits if there are such - result = UIFontDescriptor.fontDescriptorWithNameSize(fontFamily, 0); - if (symbolicTraits) { - result = result.fontDescriptorWithSymbolicTraits(symbolicTraits); + var fontFaceAttribute = ""; + + if (!symbolicTraits) { + fontFaceAttribute = "Regular"; } + else { + if (symbolicTraits & UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitBold) { + fontFaceAttribute += " Bold"; + } + if (symbolicTraits & UIFontDescriptorSymbolicTraits.UIFontDescriptorTraitItalic) { + fontFaceAttribute += " Italic"; + } + } + + var fontAttributes = NSMutableDictionary.alloc().init(); + fontAttributes.setObjectForKey(fontFamily, "NSFontFamilyAttribute"); + fontAttributes.setObjectForKey(fontFaceAttribute.trim(), "NSFontFaceAttribute"); + + result = UIFontDescriptor.fontDescriptorWithFontAttributes(fontAttributes); } else if (systemFonts.has(fontFamily)) { // This is an actual font - don't apply symbolic traits @@ -128,6 +143,9 @@ function getFontFamilyRespectingGenericFonts(fontFamily: string): string { export module ios { export function registerFont(fontFile: string) { var filePath = fs.path.join(fs.knownFolders.currentApp().path, "fonts", fontFile); + if (!fs.File.exists(filePath)) { + filePath = fs.path.join(fs.knownFolders.currentApp().path, fontFile); + } var fontData = NSFileManager.defaultManager().contentsAtPath(filePath); if (!fontData) { throw new Error("Could not load font from: " + fontFile); @@ -139,9 +157,9 @@ export module ios { throw new Error("Could not load font from: " + fontFile); } - var error = NSError.alloc().init(); + var error = new interop.Reference(); if (!CTFontManagerRegisterGraphicsFont(font, error)) { - throw new Error(error.localizedDescription); + throw new Error(CFErrorCopyDescription(error.value)); } areSystemFontSetsValid = false;