Merge pull request #663 from NativeScript/PeterStaev-master

replaced the use of fontDescriptorWithSymbolicTraits due to problems in iOS 8+.
This commit is contained in:
Nedyalko Nikolov
2015-09-04 10:18:40 +03:00
7 changed files with 73 additions and 5 deletions

View File

@ -1866,6 +1866,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="apps\transforms\package.json" />
<Content Include="apps\tests\fonts\Roboto-Bold.ttf" />
<Content Include="apps\tests\fonts\Roboto-BoldItalic.ttf" />
<Content Include="apps\tests\fonts\Roboto-Italic.ttf" />
<Content Include="apps\tests\fonts\Roboto-Regular.ttf" />
<None Include="js-libs\esprima\LICENSE.BSD" />
<Content Include="source-control.md" />
<Content Include="ui\segmented-bar\package.json">

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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((<UIButton>testView.ios).titleLabel.font.fontName.toLowerCase(), (fontName + "-" + fontNameSuffix).toLowerCase(), "native font " + weight + " " + style);
}
//TODO: If needed add tests for other platforms
}

View File

@ -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(<NSError>error.value));
}
areSystemFontSetsValid = false;