Browse Source

Support custom font in the Menu items. (#1030)

* Support custom font in the Menu items.

* Fix check for custom font.
Rui Araújo 7 years ago
parent
commit
5165389060

+ 26
- 0
android/app/src/main/java/com/reactnativenavigation/utils/TypefaceSpan.java View File

@@ -0,0 +1,26 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.graphics.Paint;
4
+import android.graphics.Typeface;
5
+import android.text.TextPaint;
6
+import android.text.style.MetricAffectingSpan;
7
+
8
+public class TypefaceSpan extends MetricAffectingSpan {
9
+    private Typeface mTypeface;
10
+
11
+    public TypefaceSpan(Typeface typeface) {
12
+        mTypeface = typeface;
13
+    }
14
+
15
+    @Override
16
+    public void updateMeasureState(TextPaint p) {
17
+        p.setTypeface(mTypeface);
18
+        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
19
+    }
20
+
21
+    @Override
22
+    public void updateDrawState(TextPaint tp) {
23
+        tp.setTypeface(mTypeface);
24
+        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
25
+    }
26
+}

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

@@ -2,6 +2,7 @@ package com.reactnativenavigation.views;
2 2
 
3 3
 import android.support.annotation.NonNull;
4 4
 import android.support.annotation.Nullable;
5
+import android.text.SpannableStringBuilder;
5 6
 import android.view.Menu;
6 7
 import android.view.MenuItem;
7 8
 import android.view.View;
@@ -9,6 +10,7 @@ import android.widget.TextView;
9 10
 
10 11
 import com.reactnativenavigation.NavigationApplication;
11 12
 import com.reactnativenavigation.params.TitleBarButtonParams;
13
+import com.reactnativenavigation.utils.TypefaceSpan;
12 14
 import com.reactnativenavigation.utils.ViewUtils;
13 15
 
14 16
 import java.util.ArrayList;
@@ -28,7 +30,7 @@ class TitleBarButton implements MenuItem.OnMenuItemClickListener {
28 30
     }
29 31
 
30 32
     MenuItem addToMenu(int index) {
31
-        MenuItem item = menu.add(Menu.NONE, Menu.NONE, index, buttonParams.label);
33
+        MenuItem item = createMenuItem(index);
32 34
         item.setShowAsAction(buttonParams.showAsAction.action);
33 35
         item.setEnabled(buttonParams.enabled);
34 36
         setIcon(item);
@@ -38,6 +40,16 @@ class TitleBarButton implements MenuItem.OnMenuItemClickListener {
38 40
         return item;
39 41
     }
40 42
 
43
+    private MenuItem createMenuItem(int index) {
44
+        if (!buttonParams.font.hasFont()) {
45
+            return menu.add(Menu.NONE, Menu.NONE, index, buttonParams.label);
46
+        }
47
+        TypefaceSpan span = new TypefaceSpan(buttonParams.font.get());
48
+        SpannableStringBuilder title = new SpannableStringBuilder(buttonParams.label);
49
+        title.setSpan(span, 0, title.length(), 0);
50
+        return menu.add(Menu.NONE, Menu.NONE, index, title);
51
+    }
52
+
41 53
     private void setIcon(MenuItem item) {
42 54
         if (hasIcon()) {
43 55
             item.setIcon(buttonParams.icon);
@@ -71,7 +83,7 @@ class TitleBarButton implements MenuItem.OnMenuItemClickListener {
71 83
     }
72 84
 
73 85
     private void setFont() {
74
-        if (buttonParams.font == null) {
86
+        if (!buttonParams.font.hasFont()) {
75 87
             return;
76 88
         }
77 89
         ArrayList<View> buttons = findActualTextViewInMenuByLabel();