mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
font-family implemented
This commit is contained in:
16
apps/ui-tests-app/font/tab-view.xml
Normal file
16
apps/ui-tests-app/font/tab-view.xml
Normal 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 " android:title="Tab 1 ">
|
||||
<TabViewItem.view>
|
||||
<Label text="Tab 1" />
|
||||
</TabViewItem.view>
|
||||
</TabViewItem>
|
||||
<TabViewItem ios:title="Tab 2 " android:title="Tab 2 ">
|
||||
<TabViewItem.view>
|
||||
<Label text="Tab 2" />
|
||||
</TabViewItem.view>
|
||||
</TabViewItem>
|
||||
</TabView.items>
|
||||
</TabView>
|
||||
</Page>
|
@ -417,6 +417,13 @@ export class Style extends DependencyObservable implements styling.Style {
|
||||
this._setShorthandProperty("font", value);
|
||||
}
|
||||
|
||||
get _fontInternal(): font.Font {
|
||||
return this._getValue(fontInternalProperty);
|
||||
}
|
||||
set _fontInternal(value: font.Font) {
|
||||
this._setValue(fontInternalProperty, value);
|
||||
}
|
||||
|
||||
get textAlignment(): string {
|
||||
return this._getValue(textAlignmentProperty);
|
||||
}
|
||||
|
@ -533,7 +533,7 @@ function setTextDecoration(view: android.widget.TextView, value: string) {
|
||||
view.setPaintFlags(flags);
|
||||
} else {
|
||||
view.setPaintFlags(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setTextTransform(view: android.widget.TextView, value: string) {
|
||||
@ -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() {
|
||||
style.registerHandler(style.colorProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||
TabViewStyler.setColorProperty,
|
||||
TabViewStyler.resetColorProperty,
|
||||
TabViewStyler.getColorProperty), "TabView");
|
||||
|
||||
style.registerHandler(style.fontInternalProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||
TabViewStyler.setFontInternalProperty,
|
||||
TabViewStyler.resetFontInternalProperty,
|
||||
TabViewStyler.getNativeFontInternalValue), "TabView");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -921,18 +921,53 @@ export class TabViewStyler implements definition.stylers.Styler {
|
||||
// color
|
||||
private static setColorProperty(view: view.View, newValue: any) {
|
||||
var tab = <tabView.TabView>view;
|
||||
tab._updateIOSTabBarColors();
|
||||
tab._updateIOSTabBarColorsAndFonts();
|
||||
}
|
||||
|
||||
private static resetColorProperty(view: view.View, nativeValue: any) {
|
||||
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() {
|
||||
style.registerHandler(style.colorProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||
TabViewStyler.setColorProperty,
|
||||
TabViewStyler.resetColorProperty), "TabView");
|
||||
|
||||
style.registerHandler(style.fontInternalProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||
TabViewStyler.setFontInternalProperty,
|
||||
TabViewStyler.resetFontInternalProperty,
|
||||
TabViewStyler.getNativeFontValue), "TabView");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -274,7 +274,7 @@ export class TabView extends view.View implements definition.TabView, view.AddAr
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public _updateIOSTabBarColors(): void {
|
||||
public _updateIOSTabBarColorsAndFonts(): void {
|
||||
// iOS sepcific
|
||||
}
|
||||
}
|
2
ui/tab-view/tab-view.d.ts
vendored
2
ui/tab-view/tab-view.d.ts
vendored
@ -101,7 +101,7 @@ declare module "ui/tab-view" {
|
||||
|
||||
//@private
|
||||
_getAndroidTabView(): any /* org.nativescript.widgets.TabLayout */;
|
||||
_updateIOSTabBarColors(): void;
|
||||
_updateIOSTabBarColorsAndFonts(): void;
|
||||
//@endprivate
|
||||
}
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ export class TabViewItem extends common.TabViewItem {
|
||||
|
||||
function selectedColorPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
var tabView = <TabView>data.object;
|
||||
tabView._updateIOSTabBarColors();
|
||||
tabView._updateIOSTabBarColorsAndFonts();
|
||||
}
|
||||
(<proxy.PropertyMetadata>common.TabView.selectedColorProperty.metadata).onSetNativeValue = selectedColorPropertyChanged;
|
||||
|
||||
@ -322,14 +322,14 @@ export class TabView extends common.TabView {
|
||||
(heightMode === utils.layout.UNSPECIFIED) ? Number.POSITIVE_INFINITY : height));
|
||||
}
|
||||
|
||||
public _updateIOSTabBarColors(): void {
|
||||
public _updateIOSTabBarColorsAndFonts(): void {
|
||||
if (!this.items) {
|
||||
return;
|
||||
}
|
||||
|
||||
var tabBar = this.ios.tabBar;
|
||||
|
||||
tabBar.tintColor = this.selectedColor ? this.selectedColor.ios : null;
|
||||
tabBar.tintColor = this.selectedColor ? this.selectedColor.ios : null;
|
||||
var states = getTitleAttributesForStates(this);
|
||||
|
||||
for (var i = 0; i < this.items.length; i++) {
|
||||
@ -354,8 +354,13 @@ function getTitleAttributesForStates(tabView: TabView): { normalState: any, sele
|
||||
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 {
|
||||
normalState: normalState,
|
||||
selectedState: selectedState
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user