Browse Source

Subtitle and Title alignment

Guy Carmeli 6 years ago
parent
commit
bcf21c79e1

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TitleOptions.java View File

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

+ 2
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java View File

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

+ 16
- 6
lib/android/app/src/main/java/com/reactnativenavigation/utils/UiUtils.java View File

8
 import android.os.Build;
8
 import android.os.Build;
9
 import android.os.Handler;
9
 import android.os.Handler;
10
 import android.os.Looper;
10
 import android.os.Looper;
11
+import android.support.annotation.NonNull;
11
 import android.util.DisplayMetrics;
12
 import android.util.DisplayMetrics;
12
 import android.view.View;
13
 import android.view.View;
13
 import android.view.ViewTreeObserver;
14
 import android.view.ViewTreeObserver;
38
 	}
39
 	}
39
 
40
 
40
 	public static float getWindowHeight(Context context) {
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
     public static int getStatusBarHeight(Context context) {
59
     public static int getStatusBarHeight(Context context) {
50
         if (statusBarHeight > 0) {
60
         if (statusBarHeight > 0) {
51
             return statusBarHeight;
61
             return statusBarHeight;

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

12
 import com.reactnativenavigation.parse.Alignment;
12
 import com.reactnativenavigation.parse.Alignment;
13
 import com.reactnativenavigation.parse.params.Button;
13
 import com.reactnativenavigation.parse.params.Button;
14
 import com.reactnativenavigation.parse.params.Color;
14
 import com.reactnativenavigation.parse.params.Color;
15
+import com.reactnativenavigation.utils.UiUtils;
15
 import com.reactnativenavigation.utils.ViewUtils;
16
 import com.reactnativenavigation.utils.ViewUtils;
16
 import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
17
 import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
17
 import com.reactnativenavigation.viewcontrollers.TitleBarReactViewController;
18
 import com.reactnativenavigation.viewcontrollers.TitleBarReactViewController;
77
         if (titleTextView != null) titleTextView.setTypeface(typeface);
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
     public void setSubtitleTypeface(Typeface typeface) {
87
     public void setSubtitleTypeface(Typeface typeface) {
81
         TextView subtitleTextView = findSubtitleTextView();
88
         TextView subtitleTextView = findSubtitleTextView();
82
         if (subtitleTextView != null) subtitleTextView.setTypeface(typeface);
89
         if (subtitleTextView != null) subtitleTextView.setTypeface(typeface);
87
         if (subtitleTextView != null) subtitleTextView.setTextSize(size);
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
     @Nullable
113
     @Nullable
91
     public TextView findTitleTextView() {
114
     public TextView findTitleTextView() {
92
         List<TextView> children = ViewUtils.findChildrenByClass(this, TextView.class, textView -> textView.getText().equals(getTitle()));
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 View File

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

+ 3
- 1
playground/src/screens/OptionsScreen.js View File

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