font-family implemented

This commit is contained in:
Vladimir Enchev
2015-12-10 12:03:18 +02:00
parent 06c164c747
commit 3650a6f4fe
7 changed files with 133 additions and 9 deletions

View File

@ -0,0 +1,16 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd">
<TabView style="font-family: FontAwesome;color: red;" selectedColor="green">
<TabView.items>
<TabViewItem ios:title="Tab 1 &#xf179;" android:title="Tab 1 &#xf17b;">
<TabViewItem.view>
<Label text="Tab 1" />
</TabViewItem.view>
</TabViewItem>
<TabViewItem ios:title="Tab 2 &#xf179;" android:title="Tab 2 &#xf17b;">
<TabViewItem.view>
<Label text="Tab 2" />
</TabViewItem.view>
</TabViewItem>
</TabView.items>
</TabView>
</Page>

View File

@ -417,6 +417,13 @@ export class Style extends DependencyObservable implements styling.Style {
this._setShorthandProperty("font", value); this._setShorthandProperty("font", value);
} }
get _fontInternal(): font.Font {
return this._getValue(fontInternalProperty);
}
set _fontInternal(value: font.Font) {
this._setValue(fontInternalProperty, value);
}
get textAlignment(): string { get textAlignment(): string {
return this._getValue(textAlignmentProperty); return this._getValue(textAlignmentProperty);
} }

View File

@ -1016,11 +1016,72 @@ export class TabViewStyler implements definition.stylers.Styler {
} }
} }
// font
private static setFontInternalProperty(view: view.View, newValue: any, nativeValue: any) {
var tab = <tabView.TabView>view;
var fontValue = <font.Font>newValue;
var typeface = fontValue.getAndroidTypeface();
if (tab.items && tab.items.length > 0) {
var tabLayout = tab._getAndroidTabView();
for (var i = 0; i < tab.items.length; i++) {
let tv = tabLayout.getTextViewForItemAt(i);
if (typeface) {
tv.setTypeface(typeface);
}
else {
tv.setTypeface(nativeValue.typeface);
}
if (fontValue.fontSize) {
tv.setTextSize(fontValue.fontSize);
}
else {
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, nativeValue.size);
}
}
}
}
private static resetFontInternalProperty(view: view.View, nativeValue: any) {
var tab = <tabView.TabView>view;
if (tab.items && tab.items.length > 0) {
var tabLayout = tab._getAndroidTabView();
for (var i = 0; i < tab.items.length; i++) {
let tv = tabLayout.getTextViewForItemAt(i);
tv.setTypeface(nativeValue.typeface);
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, nativeValue.size);
}
}
}
private static getNativeFontInternalValue(view: view.View): any {
var tab = <tabView.TabView>view;
var tv: android.widget.TextView = tab._getAndroidTabView().getTextViewForItemAt(0);
if (tv) {
return {
typeface: tv.getTypeface(),
size: tv.getTextSize()
}
}
else {
return null;
}
}
public static registerHandlers() { public static registerHandlers() {
style.registerHandler(style.colorProperty, new stylersCommon.StylePropertyChangedHandler( style.registerHandler(style.colorProperty, new stylersCommon.StylePropertyChangedHandler(
TabViewStyler.setColorProperty, TabViewStyler.setColorProperty,
TabViewStyler.resetColorProperty, TabViewStyler.resetColorProperty,
TabViewStyler.getColorProperty), "TabView"); TabViewStyler.getColorProperty), "TabView");
style.registerHandler(style.fontInternalProperty, new stylersCommon.StylePropertyChangedHandler(
TabViewStyler.setFontInternalProperty,
TabViewStyler.resetFontInternalProperty,
TabViewStyler.getNativeFontInternalValue), "TabView");
} }
} }

View File

@ -921,18 +921,53 @@ export class TabViewStyler implements definition.stylers.Styler {
// color // color
private static setColorProperty(view: view.View, newValue: any) { private static setColorProperty(view: view.View, newValue: any) {
var tab = <tabView.TabView>view; var tab = <tabView.TabView>view;
tab._updateIOSTabBarColors(); tab._updateIOSTabBarColorsAndFonts();
} }
private static resetColorProperty(view: view.View, nativeValue: any) { private static resetColorProperty(view: view.View, nativeValue: any) {
var tab = <tabView.TabView>view; var tab = <tabView.TabView>view;
tab._updateIOSTabBarColors(); tab._updateIOSTabBarColorsAndFonts();
}
// font
private static setFontInternalProperty(view: view.View, newValue: any, nativeValue: any) {
var tab = <tabView.TabView>view;
tab._updateIOSTabBarColorsAndFonts();
}
private static resetFontInternalProperty(view: view.View, nativeValue: any) {
var tab = <tabView.TabView>view;
tab._updateIOSTabBarColorsAndFonts();
}
private static getNativeFontValue(view: view.View) {
var tab = <tabView.TabView>view;
let currentFont;
if (tab.ios && tab.ios.items && tab.ios.items.length > 0) {
let currentAttrs = tab.ios.items[0].titleTextAttributesForState(UIControlState.UIControlStateNormal);
if (currentAttrs) {
currentFont = currentAttrs.objectForKey(NSFontAttributeName);
}
}
if (!currentFont) {
currentFont = UIFont.systemFontOfSize(UIFont.labelFontSize());
}
return currentFont;
} }
public static registerHandlers() { public static registerHandlers() {
style.registerHandler(style.colorProperty, new stylersCommon.StylePropertyChangedHandler( style.registerHandler(style.colorProperty, new stylersCommon.StylePropertyChangedHandler(
TabViewStyler.setColorProperty, TabViewStyler.setColorProperty,
TabViewStyler.resetColorProperty), "TabView"); TabViewStyler.resetColorProperty), "TabView");
style.registerHandler(style.fontInternalProperty, new stylersCommon.StylePropertyChangedHandler(
TabViewStyler.setFontInternalProperty,
TabViewStyler.resetFontInternalProperty,
TabViewStyler.getNativeFontValue), "TabView");
} }
} }

View File

@ -274,7 +274,7 @@ export class TabView extends view.View implements definition.TabView, view.AddAr
return undefined; return undefined;
} }
public _updateIOSTabBarColors(): void { public _updateIOSTabBarColorsAndFonts(): void {
// iOS sepcific // iOS sepcific
} }
} }

View File

@ -101,7 +101,7 @@ declare module "ui/tab-view" {
//@private //@private
_getAndroidTabView(): any /* org.nativescript.widgets.TabLayout */; _getAndroidTabView(): any /* org.nativescript.widgets.TabLayout */;
_updateIOSTabBarColors(): void; _updateIOSTabBarColorsAndFonts(): void;
//@endprivate //@endprivate
} }
} }

View File

@ -101,7 +101,7 @@ export class TabViewItem extends common.TabViewItem {
function selectedColorPropertyChanged(data: dependencyObservable.PropertyChangeData) { function selectedColorPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var tabView = <TabView>data.object; var tabView = <TabView>data.object;
tabView._updateIOSTabBarColors(); tabView._updateIOSTabBarColorsAndFonts();
} }
(<proxy.PropertyMetadata>common.TabView.selectedColorProperty.metadata).onSetNativeValue = selectedColorPropertyChanged; (<proxy.PropertyMetadata>common.TabView.selectedColorProperty.metadata).onSetNativeValue = selectedColorPropertyChanged;
@ -322,7 +322,7 @@ export class TabView extends common.TabView {
(heightMode === utils.layout.UNSPECIFIED) ? Number.POSITIVE_INFINITY : height)); (heightMode === utils.layout.UNSPECIFIED) ? Number.POSITIVE_INFINITY : height));
} }
public _updateIOSTabBarColors(): void { public _updateIOSTabBarColorsAndFonts(): void {
if (!this.items) { if (!this.items) {
return; return;
} }
@ -354,6 +354,11 @@ function getTitleAttributesForStates(tabView: TabView): { normalState: any, sele
selectedState[UITextAttributeTextColor] = tabView.ios.tabBar.tintColor; selectedState[UITextAttributeTextColor] = tabView.ios.tabBar.tintColor;
} }
var defaultFont = UIFont.systemFontOfSize(UIFont.labelFontSize());
var font = (<any>tabView.style)._fontInternal.getUIFont(defaultFont);
normalState[NSFontAttributeName] = font;
selectedState[NSFontAttributeName] = font;
return { return {
normalState: normalState, normalState: normalState,
selectedState: selectedState selectedState: selectedState