Browse Source

Support subtitle FontFamily on size on Android (#2036)

* Support subtitle FontFamily on size on Android

Font family can be either a .ttf/.otf asset or one of the default font families:

* sans-serif (regular)
* sans-serif-light
* sans-serif-condensed
* sans-serif-thin
* sans-serif-medium

* update docs
Guy Carmeli 7 years ago
parent
commit
a379f0be3f

+ 10
- 1
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -52,8 +52,10 @@ public class StyleParams {
52 52
 
53 53
     public static class Font {
54 54
         private Typeface typeface;
55
+        String fontFamilyName;
55 56
 
56 57
         public Font(String font) {
58
+            fontFamilyName = font;
57 59
             typeface = new TypefaceLoader(font).getTypeFace();
58 60
         }
59 61
 
@@ -61,7 +63,7 @@ public class StyleParams {
61 63
         }
62 64
 
63 65
         public boolean hasFont() {
64
-            return typeface != null;
66
+            return typeface != null && fontFamilyName != null;
65 67
         }
66 68
 
67 69
         public Typeface get() {
@@ -70,6 +72,11 @@ public class StyleParams {
70 72
             }
71 73
             return typeface;
72 74
         }
75
+
76
+        @Override
77
+        public String toString() {
78
+            return fontFamilyName;
79
+        }
73 80
     }
74 81
 
75 82
     public Orientation orientation;
@@ -98,6 +105,8 @@ public class StyleParams {
98 105
     public boolean topBarTranslucent;
99 106
     public Color titleBarTitleColor;
100 107
     public Color titleBarSubtitleColor;
108
+    public int titleBarSubtitleFontSize;
109
+    public Font titleBarSubtitleFontFamily;
101 110
     public Color titleBarButtonColor;
102 111
     public Color titleBarDisabledButtonColor;
103 112
     public Font titleBarTitleFont;

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

@@ -55,6 +55,8 @@ public class StyleParamsParser {
55 55
         result.topBarBorderWidth = Float.parseFloat(params.getString("topBarBorderWidth", getDefaultTopBarBorderWidth()));
56 56
 
57 57
         result.titleBarSubtitleColor = getColor("titleBarSubtitleColor", getDefaultSubtitleBarColor());
58
+        result.titleBarSubtitleFontSize = getInt("titleBarSubtitleFontSize", getDefaultSubtitleTextFontSize());
59
+        result.titleBarSubtitleFontFamily = getFont("titleBarSubtitleFontFamily", getDefaultSubtitleFontFamily());
58 60
         result.titleBarButtonColor = getColor("titleBarButtonColor", getTitleBarButtonColor());
59 61
         result.titleBarDisabledButtonColor = getColor("titleBarDisabledButtonColor", getTitleBarDisabledButtonColor());
60 62
         result.titleBarTitleFont = getFont("titleBarTitleFontFamily", getDefaultTitleTextFontFamily());
@@ -282,6 +284,14 @@ public class StyleParamsParser {
282 284
         return AppStyle.appStyle == null ? -1 : AppStyle.appStyle.titleBarTitleFontSize;
283 285
     }
284 286
 
287
+    private int getDefaultSubtitleTextFontSize() {
288
+        return AppStyle.appStyle == null ? -1 : AppStyle.appStyle.titleBarSubtitleFontSize;
289
+    }
290
+
291
+    private StyleParams.Font getDefaultSubtitleFontFamily() {
292
+        return AppStyle.appStyle == null ? new StyleParams.Font() : AppStyle.appStyle.titleBarSubtitleFontFamily;
293
+    }
294
+
285 295
     private boolean getDefaultTitleTextFontBold() {
286 296
         return AppStyle.appStyle != null && AppStyle.appStyle.titleBarTitleFontBold;
287 297
     }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

@@ -135,7 +135,7 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
135 135
             topBar.setReactView(screenParams.styleParams);
136 136
         } else {
137 137
             topBar.setTitle(screenParams.title, styleParams);
138
-            topBar.setSubtitle(screenParams.subtitle);
138
+            topBar.setSubtitle(screenParams.subtitle, styleParams);
139 139
         }
140 140
     }
141 141
 
@@ -243,7 +243,7 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
243 243
     }
244 244
 
245 245
     public void setTitleBarSubtitle(String subtitle) {
246
-        topBar.setSubtitle(subtitle);
246
+        topBar.setSubtitle(subtitle, styleParams);
247 247
     }
248 248
 
249 249
     public void setTitleBarRightButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

@@ -113,7 +113,7 @@ public class ViewUtils {
113 113
     public static <T> T findChildByClass(ViewGroup root, Class clazz, Matcher<T> matcher) {
114 114
         for (int i = 0; i < root.getChildCount(); i++) {
115 115
             View view = root.getChildAt(i);
116
-            if (clazz.isAssignableFrom(view.getClass())) {
116
+            if (clazz.isAssignableFrom(view.getClass()) && (matcher == null || matcher.match((T) view))) {
117 117
                 return (T) view;
118 118
             }
119 119
 

+ 37
- 0
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

@@ -9,6 +9,7 @@ import android.graphics.Typeface;
9 9
 import android.support.annotation.Nullable;
10 10
 import android.support.v7.widget.ActionMenuView;
11 11
 import android.support.v7.widget.Toolbar;
12
+import android.text.TextUtils;
12 13
 import android.view.Menu;
13 14
 import android.view.View;
14 15
 import android.view.animation.AccelerateDecelerateInterpolator;
@@ -79,6 +80,8 @@ public class TitleBar extends Toolbar {
79 80
         setTitleTextFontSize(params);
80 81
         setTitleTextFontWeight(params);
81 82
         setSubtitleTextColor(params);
83
+        setSubtitleFontSize(params);
84
+        setSubtitleFont(params);
82 85
         colorOverflowButton(params);
83 86
         setBackground(params);
84 87
         centerTitle(params);
@@ -90,9 +93,32 @@ public class TitleBar extends Toolbar {
90 93
 
91 94
     public void setTitle(String title, StyleParams styleParams) {
92 95
         setTitle(title);
96
+        setTitleTextFont(styleParams);
93 97
         centerTitle(styleParams);
94 98
     }
95 99
 
100
+    public void setSubtitle(CharSequence subtitle, StyleParams styleParams) {
101
+        super.setSubtitle(subtitle);
102
+        setSubtitleFontSize(styleParams);
103
+        setSubtitleFont(styleParams);
104
+    }
105
+
106
+    private void setSubtitleFontSize(StyleParams params) {
107
+        TextView subtitleView = getSubtitleView();
108
+        if (subtitleView != null && params.titleBarSubtitleFontSize > 0) {
109
+            subtitleView.setTextSize(params.titleBarSubtitleFontSize);
110
+        }
111
+    }
112
+
113
+    private void setSubtitleFont(StyleParams params) {
114
+        if (params.titleBarSubtitleFontFamily.hasFont()) {
115
+            TextView subtitleView = getSubtitleView();
116
+            if (subtitleView != null) {
117
+                subtitleView.setTypeface(params.titleBarSubtitleFontFamily.get());
118
+            }
119
+        }
120
+    }
121
+
96 122
     private void centerTitle(final StyleParams params) {
97 123
         final View titleView = getTitleView();
98 124
         if (titleView == null) {
@@ -280,6 +306,17 @@ public class TitleBar extends Toolbar {
280 306
         });
281 307
     }
282 308
 
309
+    @Nullable
310
+    private TextView getSubtitleView() {
311
+        if (TextUtils.isEmpty(getSubtitle())) return null;
312
+        return ViewUtils.findChildByClass(this, TextView.class, new ViewUtils.Matcher<TextView>() {
313
+            @Override
314
+            public boolean match(TextView child) {
315
+                return child.getText().equals(getSubtitle());
316
+            }
317
+        });
318
+    }
319
+
283 320
     public void setButtonColor(StyleParams.Color titleBarButtonColor) {
284 321
         if (!titleBarButtonColor.hasColor()) {
285 322
             return;

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -93,8 +93,8 @@ public class TopBar extends AppBarLayout {
93 93
         titleBar.setTitle(title, styleParams);
94 94
     }
95 95
 
96
-    public void setSubtitle(String subtitle) {
97
-        titleBar.setSubtitle(subtitle);
96
+    public void setSubtitle(String subtitle, StyleParams styleParams) {
97
+        titleBar.setSubtitle(subtitle, styleParams);
98 98
     }
99 99
 
100 100
     public void setReactView(@NonNull StyleParams styleParams) {

+ 2
- 1
docs/styling-the-navigator.md View File

@@ -68,7 +68,8 @@ this.props.navigator.setStyle({
68 68
   statusBarHidden: false, // make the status bar hidden regardless of nav bar state
69 69
   statusBarTextColorScheme: 'dark', // text color of status bar, 'dark' / 'light' (remembered across pushes)
70 70
   navBarSubtitleColor: 'red', // subtitle color
71
-  navBarSubtitleFontFamily: 'font-name', // subtitle font
71
+  navBarSubtitleFontFamily: 'font-name', // subtitle font, 'sans-serif-thin' for example
72
+  navBarSubtitleFontSize: 13, // subtitle font size
72 73
   screenBackgroundColor: 'white', // Default screen color, visible before the actual react view is rendered
73 74
   orientation: 'portrait' // Sets a specific orientation to a modal and all screens pushed to it. Default: 'auto'. Supported values: 'auto', 'landscape', 'portrait'
74 75
   disabledButtonColor: '#ff0000' // chnaged the navigation bar button text color when disabled.

+ 2
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -162,6 +162,8 @@ function convertStyleParams(originalStyleObject) {
162 162
     titleBarHideOnScroll: originalStyleObject.navBarHideOnScroll,
163 163
     titleBarTitleColor: processColor(originalStyleObject.navBarTextColor),
164 164
     titleBarSubtitleColor: processColor(originalStyleObject.navBarSubtitleColor),
165
+    titleBarSubtitleFontSize: originalStyleObject.navBarSubtitleFontSize,
166
+    titleBarSubtitleFontFamily: originalStyleObject.navBarSubtitleFontFamily,
165 167
     titleBarButtonColor: processColor(originalStyleObject.navBarButtonColor),
166 168
     titleBarDisabledButtonColor: processColor(originalStyleObject.titleBarDisabledButtonColor),
167 169
     titleBarTitleFontFamily: originalStyleObject.navBarTextFontFamily,