Parcourir la source

Subtitle and Title alignment

Guy Carmeli il y a 6 ans
Parent
révision
bcf21c79e1

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TitleOptions.java Voir le fichier

@@ -30,6 +30,7 @@ public class TitleOptions {
30 30
         options.color = ColorParser.parse(json, "color");
31 31
         options.fontSize = FractionParser.parse(json, "fontSize");
32 32
         options.fontFamily = typefaceManager.getTypeFace(json.optString("fontFamily", ""));
33
+        options.alignment = Alignment.fromString(TextParser.parse(json, "alignment").get(""));
33 34
         options.component = TextParser.parse(json, "component");
34 35
         options.componentAlignment = Alignment.fromString(TextParser.parse(json, "componentAlignment").get(""));
35 36
 
@@ -41,6 +42,7 @@ public class TitleOptions {
41 42
     public Text text = new NullText();
42 43
     public Color color = new NullColor();
43 44
     public Fraction fontSize = new NullFraction();
45
+    public Alignment alignment = Alignment.Default;
44 46
     @Nullable public Typeface fontFamily;
45 47
     public Text component = new NullText();
46 48
     public Alignment componentAlignment = Alignment.Default;
@@ -50,6 +52,7 @@ public class TitleOptions {
50 52
         if (other.color.hasValue()) color = other.color;
51 53
         if (other.fontSize.hasValue()) fontSize = other.fontSize;
52 54
         if (other.fontFamily != null) fontFamily = other.fontFamily;
55
+        if (other.alignment != Alignment.Default) alignment = other.alignment;
53 56
         if (other.component.hasValue()) component = other.component;
54 57
         if (other.componentAlignment != Alignment.Default) componentAlignment = other.componentAlignment;
55 58
         validate(this);
@@ -60,6 +63,7 @@ public class TitleOptions {
60 63
         if (!color.hasValue()) color = defaultOptions.color;
61 64
         if (!fontSize.hasValue()) fontSize = defaultOptions.fontSize;
62 65
         if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
66
+        if (alignment == Alignment.Default) alignment = defaultOptions.alignment;
63 67
         if (!component.hasValue()) component = defaultOptions.component;
64 68
         if (componentAlignment == Alignment.Default) componentAlignment = defaultOptions.componentAlignment;
65 69
         validate(this);

+ 2
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java Voir le fichier

@@ -51,11 +51,13 @@ public class OptionsPresenter {
51 51
         topBar.setTitleFontSize(options.title.fontSize.get(defaultTitleFontSize));
52 52
         topBar.setTitleTextColor(options.title.color.get(DEFAULT_TITLE_COLOR));
53 53
         topBar.setTitleTypeface(options.title.fontFamily);
54
+        topBar.setTitleAlignment(options.title.alignment);
54 55
 
55 56
         topBar.setSubtitle(options.subtitle.text.get(""));
56 57
         topBar.setSubtitleFontSize(options.subtitle.fontSize.get(defaultSubtitleFontSize));
57 58
         topBar.setSubtitleColor(options.subtitle.color.get(DEFAULT_SUBTITLE_COLOR));
58 59
         topBar.setSubtitleFontFamily(options.subtitle.fontFamily);
60
+        topBar.setSubtitleAlignment(options.subtitle.alignment);
59 61
 
60 62
         topBar.setBackgroundColor(options.background.color);
61 63
         topBar.setBackgroundComponent(options.background.component);

+ 16
- 6
lib/android/app/src/main/java/com/reactnativenavigation/utils/UiUtils.java Voir le fichier

@@ -8,6 +8,7 @@ import android.graphics.drawable.Drawable;
8 8
 import android.os.Build;
9 9
 import android.os.Handler;
10 10
 import android.os.Looper;
11
+import android.support.annotation.NonNull;
11 12
 import android.util.DisplayMetrics;
12 13
 import android.view.View;
13 14
 import android.view.ViewTreeObserver;
@@ -38,14 +39,23 @@ public class UiUtils {
38 39
 	}
39 40
 
40 41
 	public static float getWindowHeight(Context context) {
41
-		DisplayMetrics metrics = new DisplayMetrics();
42
-		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
43
-		if (windowManager != null) {
44
-			windowManager.getDefaultDisplay().getMetrics(metrics);
45
-		}
46
-		return metrics.heightPixels;
42
+        return getDisplayMetrics(context).heightPixels;
47 43
 	}
48 44
 
45
+    public static float getWindowWidth(Context context) {
46
+        return getDisplayMetrics(context).widthPixels;
47
+    }
48
+
49
+    @NonNull
50
+    private static DisplayMetrics getDisplayMetrics(Context context) {
51
+        DisplayMetrics metrics = new DisplayMetrics();
52
+        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
53
+        if (windowManager != null) {
54
+            windowManager.getDefaultDisplay().getMetrics(metrics);
55
+        }
56
+        return metrics;
57
+    }
58
+
49 59
     public static int getStatusBarHeight(Context context) {
50 60
         if (statusBarHeight > 0) {
51 61
             return statusBarHeight;

+ 23
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java Voir le fichier

@@ -12,6 +12,7 @@ import android.widget.TextView;
12 12
 import com.reactnativenavigation.parse.Alignment;
13 13
 import com.reactnativenavigation.parse.params.Button;
14 14
 import com.reactnativenavigation.parse.params.Color;
15
+import com.reactnativenavigation.utils.UiUtils;
15 16
 import com.reactnativenavigation.utils.ViewUtils;
16 17
 import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
17 18
 import com.reactnativenavigation.viewcontrollers.TitleBarReactViewController;
@@ -77,6 +78,12 @@ public class TitleBar extends Toolbar {
77 78
         if (titleTextView != null) titleTextView.setTypeface(typeface);
78 79
     }
79 80
 
81
+    public void setTitleAlignment(Alignment alignment) {
82
+        TextView title = findTitleTextView();
83
+        if (title == null) return;
84
+        alignTextView(alignment, title);
85
+    }
86
+
80 87
     public void setSubtitleTypeface(Typeface typeface) {
81 88
         TextView subtitleTextView = findSubtitleTextView();
82 89
         if (subtitleTextView != null) subtitleTextView.setTypeface(typeface);
@@ -87,6 +94,22 @@ public class TitleBar extends Toolbar {
87 94
         if (subtitleTextView != null) subtitleTextView.setTextSize(size);
88 95
     }
89 96
 
97
+    public void setSubtitleAlignment(Alignment alignment) {
98
+        TextView subtitle = findSubtitleTextView();
99
+        if (subtitle == null) return;
100
+        alignTextView(alignment, subtitle);
101
+    }
102
+
103
+    private void alignTextView(Alignment alignment, TextView view) {
104
+        view.post(() -> {
105
+            if (alignment == Alignment.Center) {
106
+                view.setX((getWidth() - view.getWidth()) / 2);
107
+            } else {
108
+                view.setX(UiUtils.dpToPx(getContext(), 16));
109
+            }
110
+        });
111
+    }
112
+
90 113
     @Nullable
91 114
     public TextView findTitleTextView() {
92 115
         List<TextView> children = ViewUtils.findChildrenByClass(this, TextView.class, textView -> textView.getText().equals(getTitle()));

+ 8
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/topbar/TopBar.java Voir le fichier

@@ -95,6 +95,10 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
95 95
         titleBar.setSubtitleFontSize(size);
96 96
     }
97 97
 
98
+    public void setSubtitleAlignment(Alignment alignment) {
99
+        titleBar.setSubtitleAlignment(alignment);
100
+    }
101
+
98 102
     public void setTestId(String testId) {
99 103
         setTag(testId);
100 104
     }
@@ -111,6 +115,10 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
111 115
         titleBar.setTitleTypeface(typeface);
112 116
     }
113 117
 
118
+    public void setTitleAlignment(Alignment alignment) {
119
+        titleBar.setTitleAlignment(alignment);
120
+    }
121
+
114 122
     public void setTitleComponent(String componentName, Alignment alignment) {
115 123
         titleBar.setComponent(componentName, alignment);
116 124
     }

+ 3
- 1
playground/src/screens/OptionsScreen.js Voir le fichier

@@ -21,13 +21,15 @@ class OptionsScreen extends Component {
21 21
           text: 'Static Title',
22 22
           color: 'black',
23 23
           fontSize: 16,
24
+          alignment: 'center',
24 25
           fontFamily: 'HelveticaNeue-Italic',
25 26
           largeTitle: false
26 27
         },
27 28
         subtitle: {
28 29
           text: 'Static Subtitle',
29 30
           color: 'red',
30
-          fontFamily: 'HelveticaNeue-Italic'
31
+          fontFamily: 'HelveticaNeue-Italic',
32
+          alignment: 'center'
31 33
         },
32 34
         background: {
33 35
           component: 'TopBarBackground'