Skip to content

Commit

Permalink
fix: dont default to Font.default (#8401)
Browse files Browse the repository at this point in the history
* fix: dont default to Font.default

This would cause a font to be set for any label even when using default system font.
This will also cause a typeface which is pretty long

* lint: lint fixes

* fix: added null font guards

* fix: Used default bold for TabView

Co-authored-by: Vasko <v.trifonov@gmail.com>
  • Loading branch information
farfromrefug and vtrifonov committed Apr 3, 2020
1 parent 56f6626 commit ad9daa8
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 26 deletions.
1 change: 1 addition & 0 deletions nativescript-core/image-source/image-source.android.ts
Expand Up @@ -176,6 +176,7 @@ export class ImageSource implements ImageSourceDefinition {
}

static fromFontIconCodeSync(source: string, font: Font, color: Color): ImageSource {
font = font || Font.default;
const paint = new android.graphics.Paint();
paint.setTypeface(font.getAndroidTypeface());
paint.setAntiAlias(true);
Expand Down
1 change: 1 addition & 0 deletions nativescript-core/image-source/image-source.ios.ts
Expand Up @@ -165,6 +165,7 @@ export class ImageSource implements ImageSourceDefinition {
}

static fromFontIconCodeSync(source: string, font: Font, color: Color): ImageSource {
font = font || Font.default;
let fontSize = layout.toDevicePixels(font.fontSize);
if (!fontSize) {
// TODO: Consider making 36 font size as default for optimal look on TabView and ActionBar
Expand Down
Expand Up @@ -654,7 +654,7 @@ export class BottomNavigation extends TabNavigationBase {
}

const target = tabStripItem.image;
const font = target.style.fontInternal;
const font = target.style.fontInternal || Font.default;
if (!color) {
color = target.style.color;
}
Expand Down Expand Up @@ -774,7 +774,7 @@ export class BottomNavigation extends TabNavigationBase {

const defaultTabItemFontSize = 10;
const tabItemFontSize = view.style.fontSize || defaultTabItemFontSize;
const font: UIFont = view.style.fontInternal.getUIFont(UIFont.systemFontOfSize(tabItemFontSize));
const font: UIFont = (view.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(tabItemFontSize));
const tabItemTextColor = view.style.color;
const textColor = tabItemTextColor instanceof Color ? tabItemTextColor.ios : null;
let attributes: any = { [NSFontAttributeName]: font };
Expand Down
13 changes: 8 additions & 5 deletions nativescript-core/ui/styling/style-properties.ts
Expand Up @@ -1154,12 +1154,12 @@ opacityProperty.register(Style);
export const colorProperty = new InheritedCssProperty<Style, Color>({ name: "color", cssName: "color", equalityComparer: Color.equals, valueConverter: (v) => new Color(v) });
colorProperty.register(Style);

export const fontInternalProperty = new CssProperty<Style, Font>({ name: "fontInternal", cssName: "_fontInternal", defaultValue: Font.default });
export const fontInternalProperty = new CssProperty<Style, Font>({ name: "fontInternal", cssName: "_fontInternal" });
fontInternalProperty.register(Style);

export const fontFamilyProperty = new InheritedCssProperty<Style, string>({
name: "fontFamily", cssName: "font-family", affectsLayout: isIOS, valueChanged: (target, oldValue, newValue) => {
let currentFont = target.fontInternal;
let currentFont = target.fontInternal || Font.default;
if (currentFont.fontFamily !== newValue) {
const newFont = currentFont.withFontFamily(newValue);
target.fontInternal = Font.equals(Font.default, newFont) ? unsetValue : newFont;
Expand All @@ -1170,7 +1170,10 @@ fontFamilyProperty.register(Style);

export const fontSizeProperty = new InheritedCssProperty<Style, number>({
name: "fontSize", cssName: "font-size", affectsLayout: isIOS, valueChanged: (target, oldValue, newValue) => {
let currentFont = target.fontInternal;
if (target.viewRef["handleFontSize"] === true) {
return;
}
let currentFont = target.fontInternal || Font.default;
if (currentFont.fontSize !== newValue) {
const newFont = currentFont.withFontSize(newValue);
target.fontInternal = Font.equals(Font.default, newFont) ? unsetValue : newFont;
Expand All @@ -1182,7 +1185,7 @@ fontSizeProperty.register(Style);

export const fontStyleProperty = new InheritedCssProperty<Style, FontStyle>({
name: "fontStyle", cssName: "font-style", affectsLayout: isIOS, defaultValue: FontStyle.NORMAL, valueConverter: FontStyle.parse, valueChanged: (target, oldValue, newValue) => {
let currentFont = target.fontInternal;
let currentFont = target.fontInternal || Font.default;
if (currentFont.fontStyle !== newValue) {
const newFont = currentFont.withFontStyle(newValue);
target.fontInternal = Font.equals(Font.default, newFont) ? unsetValue : newFont;
Expand All @@ -1193,7 +1196,7 @@ fontStyleProperty.register(Style);

export const fontWeightProperty = new InheritedCssProperty<Style, FontWeight>({
name: "fontWeight", cssName: "font-weight", affectsLayout: isIOS, defaultValue: FontWeight.NORMAL, valueConverter: FontWeight.parse, valueChanged: (target, oldValue, newValue) => {
let currentFont = target.fontInternal;
let currentFont = target.fontInternal || Font.default;
if (currentFont.fontWeight !== newValue) {
const newFont = currentFont.withFontWeight(newValue);
target.fontInternal = Font.equals(Font.default, newFont) ? unsetValue : newFont;
Expand Down
2 changes: 1 addition & 1 deletion nativescript-core/ui/tab-view/tab-view.ios.ts
Expand Up @@ -587,7 +587,7 @@ function getTitleAttributesForStates(tabView: TabView): TabStates {

const defaultTabItemFontSize = 10;
const tabItemFontSize = tabView.style.tabTextFontSize || defaultTabItemFontSize;
const font: UIFont = tabView.style.fontInternal.getUIFont(UIFont.systemFontOfSize(tabItemFontSize));
const font: UIFont = (tabView.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(tabItemFontSize));
const tabItemTextColor = tabView.style.tabTextColor;
const textColor = tabItemTextColor instanceof Color ? tabItemTextColor.ios : null;
result.normalState = { [NSFontAttributeName]: font };
Expand Down
6 changes: 3 additions & 3 deletions nativescript-core/ui/tabs/tabs.ios.ts
Expand Up @@ -824,7 +824,7 @@ export class Tabs extends TabsBase {
}

const target = tabStripItem.image;
const font = target.style.fontInternal;
const font = target.style.fontInternal || Font.default;
if (!color) {
color = target.style.color;
}
Expand Down Expand Up @@ -997,7 +997,7 @@ export class Tabs extends TabsBase {
public setTabBarFontInternal(value: Font): void {
const defaultTabItemFontSize = 10;
const tabItemFontSize = this.tabStrip.style.fontSize || defaultTabItemFontSize;
const font: UIFont = this.tabStrip.style.fontInternal.getUIFont(UIFont.systemFontOfSize(tabItemFontSize));
const font: UIFont = (this.tabStrip.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(tabItemFontSize));

this._ios.tabBar.unselectedItemTitleFont = font;
this._ios.tabBar.selectedItemTitleFont = font;
Expand Down Expand Up @@ -1194,7 +1194,7 @@ export class Tabs extends TabsBase {

const defaultTabItemFontSize = 10;
const tabItemFontSize = view.style.fontSize || defaultTabItemFontSize;
const font: UIFont = view.style.fontInternal.getUIFont(UIFont.systemFontOfSize(tabItemFontSize));
const font: UIFont = (view.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(tabItemFontSize));

this.viewController.tabBar.unselectedItemTitleFont = font;
this.viewController.tabBar.selectedItemTitleFont = font;
Expand Down
@@ -1,4 +1,5 @@
import tabViewModule = require("@nativescript/core/ui/tab-view");
import { Font } from "@nativescript/core/ui/styling/font";

export function getNativeTabCount(tabView: tabViewModule.TabView): number {
if (!tabView.ios.viewControllers) {
Expand Down Expand Up @@ -30,5 +31,5 @@ export function getNativeFont(tabView: tabViewModule.TabView): UIFont {
}

export function getOriginalFont(tabView: tabViewModule.TabView): UIFont {
return tabView.style.fontInternal.getUIFont(UIFont.systemFontOfSize(10));
return (tabView.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(10));
}
3 changes: 2 additions & 1 deletion tests/app/ui/tab-view/tab-view-tests-native.ios.ts
@@ -1,4 +1,5 @@
import tabViewModule = require("@nativescript/core/ui/tab-view");
import { Font } from "@nativescript/core/ui/styling/font";

export function getNativeTabCount(tabView: tabViewModule.TabView): number {
if (!tabView.ios.viewControllers) {
Expand Down Expand Up @@ -30,5 +31,5 @@ export function getNativeFont(tabView: tabViewModule.TabView): UIFont {
}

export function getOriginalFont(tabView: tabViewModule.TabView): UIFont {
return tabView.style.fontInternal.getUIFont(UIFont.systemFontOfSize(10));
return (tabView.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(10));
}
3 changes: 2 additions & 1 deletion tests/app/ui/tabs/tabs-tests-native.ios.ts
@@ -1,4 +1,5 @@
import { Tabs } from "@nativescript/core/ui/tabs";
import { Font } from "@nativescript/core/ui/styling/font";

// TODO: Should we add getCount to UIPageViewController???
export function getNativeTabCount(tabView: Tabs): number {
Expand Down Expand Up @@ -39,5 +40,5 @@ export function getNativeFont(tabView: Tabs): UIFont {
}

export function getOriginalFont(tabView: Tabs): UIFont {
return tabView.style.fontInternal.getUIFont(UIFont.systemFontOfSize(10));
return (tabView.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(10));
}
Expand Up @@ -178,7 +178,6 @@ protected View createDefaultTabView(Context context, TabItemSpec tabItem) {
titleTextView.setGravity(Gravity.CENTER);
titleTextView.setMaxWidth((int) (ITEM_TEXT_MAX_WIDTH * density));
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, ITEM_TEXT_SIZE_SP);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
titleTextView.setEllipsize(TextUtils.TruncateAt.END);
titleTextView.setMaxLines(1);
titleTextView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Expand Down
Expand Up @@ -160,7 +160,7 @@ public float getTabTextFontSize(){
* {@link TabLayout} you are required to set any
* {@link ViewPager.OnPageChangeListener} through this method. This is so
* that the layout can update it's scroll position correctly.
*
*
* @see ViewPager#setOnPageChangeListener(ViewPager.OnPageChangeListener)
*/
public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
Expand Down Expand Up @@ -196,25 +196,25 @@ public void updateItemAt(int position, TabItemSpec tabItem) {
TextView textView = (TextView)ll.getChildAt(1);
this.setupItem(ll, textView, imgView, tabItem);
}

/**
* Gets the TextView for tab item at index
*/
public TextView getTextViewForItemAt(int index){
LinearLayout ll = this.getViewForItemAt(index);
return (ll != null) ? (TextView)ll.getChildAt(1) : null;
return (ll != null) ? (TextView)ll.getChildAt(1) : null;
}

/**
* Gets the LinearLayout container for tab item at index
*/
public LinearLayout getViewForItemAt(int index){
LinearLayout result = null;

if(this.mTabStrip.getChildCount() > index){
result = (LinearLayout)this.mTabStrip.getChildAt(index);
}

return result;
}

Expand Down Expand Up @@ -263,10 +263,10 @@ protected View createDefaultTabView(Context context, TabItemSpec tabItem) {
ll.addView(textView);
return ll;
}

private void setupItem(LinearLayout ll, TextView textView,ImageView imgView, TabItemSpec tabItem){
float density = getResources().getDisplayMetrics().density;

if (tabItem.iconId != 0) {
imgView.setImageResource(tabItem.iconId);
imgView.setVisibility(VISIBLE);
Expand All @@ -293,7 +293,7 @@ private void setupItem(LinearLayout ll, TextView textView,ImageView imgView, Tab
} else {
ll.setMinimumHeight((int) (SMALL_MIN_HEIGHT * density));
}

if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.width = 0;
Expand Down Expand Up @@ -438,4 +438,4 @@ public void onClick(View v) {
}
}
}
}
}
Expand Up @@ -250,7 +250,6 @@ protected View createDefaultTabView(Context context, TabItemSpec tabItem) {
textView.setGravity(Gravity.CENTER);
textView.setMaxWidth((int) (TEXT_MAX_WIDTH * density));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setMaxLines(2);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Expand Down

0 comments on commit ad9daa8

Please sign in to comment.