Просмотр исходного кода

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 лет назад
Родитель
Сommit
a379f0be3f

+ 10
- 1
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java Просмотреть файл

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

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java Просмотреть файл

55
         result.topBarBorderWidth = Float.parseFloat(params.getString("topBarBorderWidth", getDefaultTopBarBorderWidth()));
55
         result.topBarBorderWidth = Float.parseFloat(params.getString("topBarBorderWidth", getDefaultTopBarBorderWidth()));
56
 
56
 
57
         result.titleBarSubtitleColor = getColor("titleBarSubtitleColor", getDefaultSubtitleBarColor());
57
         result.titleBarSubtitleColor = getColor("titleBarSubtitleColor", getDefaultSubtitleBarColor());
58
+        result.titleBarSubtitleFontSize = getInt("titleBarSubtitleFontSize", getDefaultSubtitleTextFontSize());
59
+        result.titleBarSubtitleFontFamily = getFont("titleBarSubtitleFontFamily", getDefaultSubtitleFontFamily());
58
         result.titleBarButtonColor = getColor("titleBarButtonColor", getTitleBarButtonColor());
60
         result.titleBarButtonColor = getColor("titleBarButtonColor", getTitleBarButtonColor());
59
         result.titleBarDisabledButtonColor = getColor("titleBarDisabledButtonColor", getTitleBarDisabledButtonColor());
61
         result.titleBarDisabledButtonColor = getColor("titleBarDisabledButtonColor", getTitleBarDisabledButtonColor());
60
         result.titleBarTitleFont = getFont("titleBarTitleFontFamily", getDefaultTitleTextFontFamily());
62
         result.titleBarTitleFont = getFont("titleBarTitleFontFamily", getDefaultTitleTextFontFamily());
282
         return AppStyle.appStyle == null ? -1 : AppStyle.appStyle.titleBarTitleFontSize;
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
     private boolean getDefaultTitleTextFontBold() {
295
     private boolean getDefaultTitleTextFontBold() {
286
         return AppStyle.appStyle != null && AppStyle.appStyle.titleBarTitleFontBold;
296
         return AppStyle.appStyle != null && AppStyle.appStyle.titleBarTitleFontBold;
287
     }
297
     }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java Просмотреть файл

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

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java Просмотреть файл

113
     public static <T> T findChildByClass(ViewGroup root, Class clazz, Matcher<T> matcher) {
113
     public static <T> T findChildByClass(ViewGroup root, Class clazz, Matcher<T> matcher) {
114
         for (int i = 0; i < root.getChildCount(); i++) {
114
         for (int i = 0; i < root.getChildCount(); i++) {
115
             View view = root.getChildAt(i);
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
                 return (T) view;
117
                 return (T) view;
118
             }
118
             }
119
 
119
 

+ 37
- 0
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java Просмотреть файл

9
 import android.support.annotation.Nullable;
9
 import android.support.annotation.Nullable;
10
 import android.support.v7.widget.ActionMenuView;
10
 import android.support.v7.widget.ActionMenuView;
11
 import android.support.v7.widget.Toolbar;
11
 import android.support.v7.widget.Toolbar;
12
+import android.text.TextUtils;
12
 import android.view.Menu;
13
 import android.view.Menu;
13
 import android.view.View;
14
 import android.view.View;
14
 import android.view.animation.AccelerateDecelerateInterpolator;
15
 import android.view.animation.AccelerateDecelerateInterpolator;
79
         setTitleTextFontSize(params);
80
         setTitleTextFontSize(params);
80
         setTitleTextFontWeight(params);
81
         setTitleTextFontWeight(params);
81
         setSubtitleTextColor(params);
82
         setSubtitleTextColor(params);
83
+        setSubtitleFontSize(params);
84
+        setSubtitleFont(params);
82
         colorOverflowButton(params);
85
         colorOverflowButton(params);
83
         setBackground(params);
86
         setBackground(params);
84
         centerTitle(params);
87
         centerTitle(params);
90
 
93
 
91
     public void setTitle(String title, StyleParams styleParams) {
94
     public void setTitle(String title, StyleParams styleParams) {
92
         setTitle(title);
95
         setTitle(title);
96
+        setTitleTextFont(styleParams);
93
         centerTitle(styleParams);
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
     private void centerTitle(final StyleParams params) {
122
     private void centerTitle(final StyleParams params) {
97
         final View titleView = getTitleView();
123
         final View titleView = getTitleView();
98
         if (titleView == null) {
124
         if (titleView == null) {
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
     public void setButtonColor(StyleParams.Color titleBarButtonColor) {
320
     public void setButtonColor(StyleParams.Color titleBarButtonColor) {
284
         if (!titleBarButtonColor.hasColor()) {
321
         if (!titleBarButtonColor.hasColor()) {
285
             return;
322
             return;

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java Просмотреть файл

93
         titleBar.setTitle(title, styleParams);
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
     public void setReactView(@NonNull StyleParams styleParams) {
100
     public void setReactView(@NonNull StyleParams styleParams) {

+ 2
- 1
docs/styling-the-navigator.md Просмотреть файл

68
   statusBarHidden: false, // make the status bar hidden regardless of nav bar state
68
   statusBarHidden: false, // make the status bar hidden regardless of nav bar state
69
   statusBarTextColorScheme: 'dark', // text color of status bar, 'dark' / 'light' (remembered across pushes)
69
   statusBarTextColorScheme: 'dark', // text color of status bar, 'dark' / 'light' (remembered across pushes)
70
   navBarSubtitleColor: 'red', // subtitle color
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
   screenBackgroundColor: 'white', // Default screen color, visible before the actual react view is rendered
73
   screenBackgroundColor: 'white', // Default screen color, visible before the actual react view is rendered
73
   orientation: 'portrait' // Sets a specific orientation to a modal and all screens pushed to it. Default: 'auto'. Supported values: 'auto', 'landscape', 'portrait'
74
   orientation: 'portrait' // Sets a specific orientation to a modal and all screens pushed to it. Default: 'auto'. Supported values: 'auto', 'landscape', 'portrait'
74
   disabledButtonColor: '#ff0000' // chnaged the navigation bar button text color when disabled.
75
   disabledButtonColor: '#ff0000' // chnaged the navigation bar button text color when disabled.

+ 2
- 0
src/deprecated/platformSpecificDeprecated.android.js Просмотреть файл

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