Browse Source

GX-4416 (#2)

* adding support for android centered title and custom font

* PR feedback
Kelt 7 years ago
parent
commit
d76407745c

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -62,6 +62,8 @@ public class StyleParams {
62 62
     public Color titleBarSubtitleColor;
63 63
     public Color titleBarButtonColor;
64 64
     public Color titleBarDisabledButtonColor;
65
+    public String titleBarTitleFont;
66
+    public boolean titleBarTitleTextCentered;
65 67
     public boolean backButtonHidden;
66 68
 
67 69
     public Color topTabTextColor;

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

@@ -43,6 +43,8 @@ public class StyleParamsParser {
43 43
         result.titleBarSubtitleColor = getColor("titleBarSubtitleColor", getDefaultSubtitleBarColor());
44 44
         result.titleBarButtonColor = getColor("titleBarButtonColor", getTitleBarButtonColor());
45 45
         result.titleBarDisabledButtonColor = getColor("titleBarDisabledButtonColor", getTitleBarDisabledButtonColor());
46
+        result.titleBarTitleFont = getString("titleBarTitleFont", "");
47
+        result.titleBarTitleTextCentered = getBoolean("titleBarTitleTextCentered", false);
46 48
         result.backButtonHidden = getBoolean("backButtonHidden", getDefaultBackButtonHidden());
47 49
         result.topTabsHidden = getBoolean("topTabsHidden", getDefaultTopTabsHidden());
48 50
 
@@ -247,4 +249,9 @@ public class StyleParamsParser {
247 249
     private int getInt(String key, int defaultValue) {
248 250
         return params.containsKey(key) ? params.getInt(key) : defaultValue;
249 251
     }
252
+
253
+    private String getString(String key, String defaultValue) {
254
+        return params.containsKey(key) ? params.getString(key) : defaultValue;
255
+    }
256
+
250 257
 }

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

@@ -3,12 +3,16 @@ package com.reactnativenavigation.views;
3 3
 import android.animation.Animator;
4 4
 import android.animation.AnimatorListenerAdapter;
5 5
 import android.content.Context;
6
+import android.graphics.Point;
7
+import android.graphics.Typeface;
6 8
 import android.graphics.drawable.Drawable;
7 9
 import android.support.annotation.Nullable;
8 10
 import android.support.v7.widget.ActionMenuView;
9 11
 import android.support.v7.widget.Toolbar;
12
+import android.view.Display;
10 13
 import android.view.Menu;
11 14
 import android.view.View;
15
+import android.view.WindowManager;
12 16
 import android.view.animation.AccelerateDecelerateInterpolator;
13 17
 import android.view.animation.AccelerateInterpolator;
14 18
 import android.widget.TextView;
@@ -25,11 +29,28 @@ public class TitleBar extends Toolbar {
25 29
     private static final int TITLE_VISIBILITY_ANIMATION_DURATION = 320;
26 30
     private LeftButton leftButton;
27 31
     private ActionMenuView actionMenuView;
32
+    private boolean titleBarTitleTextCentered;
28 33
 
29 34
     public TitleBar(Context context) {
30 35
         super(context);
31 36
     }
32 37
 
38
+    @Override
39
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
40
+        super.onLayout(changed, l, t, r, b);
41
+
42
+        if (titleBarTitleTextCentered) {
43
+            WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
44
+            Display display = windowManager.getDefaultDisplay();
45
+            Point screenSize = new Point();
46
+            display.getSize(screenSize);
47
+
48
+            int[] location = new int[2];
49
+            getTitleView().getLocationOnScreen(location);
50
+            getTitleView().setTranslationX(getTitleView().getTranslationX() + (-location[0] + screenSize.x / 2 - getTitleView().getWidth() / 2));
51
+        }
52
+    }
53
+
33 54
     @Override
34 55
     public void onViewAdded(View child) {
35 56
         super.onViewAdded(child);
@@ -68,8 +89,10 @@ public class TitleBar extends Toolbar {
68 89
     }
69 90
 
70 91
     public void setStyle(StyleParams params) {
92
+        titleBarTitleTextCentered = params.titleBarTitleTextCentered;
71 93
         setVisibility(params.titleBarHidden ? GONE : VISIBLE);
72 94
         setTitleTextColor(params);
95
+        setTitleTextFont(params);
73 96
         setSubtitleTextColor(params);
74 97
         colorOverflowButton(params);
75 98
         setBackground(params);
@@ -102,6 +125,31 @@ public class TitleBar extends Toolbar {
102 125
         }
103 126
     }
104 127
 
128
+    protected void setTitleTextFont(StyleParams params) {
129
+        if (params.titleBarTitleFont == null || params.titleBarTitleFont.isEmpty()) {
130
+            return;
131
+        }
132
+
133
+        View titleView = getTitleView();
134
+
135
+        if (titleView == null || !(titleView instanceof TextView)) {
136
+            return;
137
+        }
138
+
139
+        Typeface typeface = null;
140
+
141
+        try {
142
+            typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + params.titleBarTitleFont + ".ttf");
143
+        } catch (RuntimeException re) {
144
+            try {
145
+                typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + params.titleBarTitleFont + ".otf");
146
+            } catch (RuntimeException re2) {
147
+                return;
148
+            }
149
+        }
150
+        ((TextView) titleView).setTypeface(typeface);
151
+    }
152
+
105 153
     protected void setSubtitleTextColor(StyleParams params) {
106 154
         if (params.titleBarSubtitleColor.hasColor()) {
107 155
             setSubtitleTextColor(params.titleBarSubtitleColor.getColor());

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

@@ -154,6 +154,8 @@ function convertStyleParams(originalStyleObject) {
154 154
     titleBarSubtitleColor: processColor(originalStyleObject.navBarSubtitleColor),
155 155
     titleBarButtonColor: processColor(originalStyleObject.navBarButtonColor),
156 156
     titleBarDisabledButtonColor: processColor(originalStyleObject.titleBarDisabledButtonColor),
157
+    titleBarTitleFont: originalStyleObject.navBarTitleFont,
158
+    titleBarTitleTextCentered: originalStyleObject.navBarTitleTextCentered,
157 159
     backButtonHidden: originalStyleObject.backButtonHidden,
158 160
     topTabsHidden: originalStyleObject.topTabsHidden,
159 161
     contextualMenuStatusBarColor: processColor(originalStyleObject.contextualMenuStatusBarColor),