Skip to content

Commit 0e39dea

Browse files
farnabazxwenliang
authored andcommitted
Android custome fontFamily (beefe#301)
1 parent ceb20cb commit 0e39dea

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
|wheelFlex | array | [1, 1, 1] | iOS/Android | |
2626
|pickerFontSize | number | 16 | iOS/Android | |
2727
|pickerFontColor | array | [31, 31, 31, 1] | iOS/Android | |
28+
|pickerFontFamily | string | | Android | |
2829
|pickerRowHeight | number | 24 | iOS | |
2930
|pickerData | array | | iOS/Android | |
3031
|selectedValue | array | | iOS/Android | |

android/src/main/java/com/beefe/picker/PickerViewModule.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.beefe.picker;
22

3+
import android.content.res.AssetManager;
34
import android.app.Activity;
45
import android.app.Dialog;
56
import android.graphics.Color;
67
import android.graphics.PixelFormat;
8+
import android.graphics.Typeface;
79
import android.support.annotation.Nullable;
810
import android.text.TextUtils;
911
import android.view.Gravity;
@@ -75,6 +77,10 @@
7577
*/
7678

7779
public class PickerViewModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
80+
81+
private static final String FONTS = "fonts/";
82+
private static final String OTF = ".otf";
83+
private static final String TTF = ".ttf";
7884

7985
private static final String REACT_CLASS = "BEEPickerManager";
8086

@@ -104,6 +110,8 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
104110
private static final String PICKER_TEXT_SIZE = "pickerFontSize";
105111
private static final String PICKER_TEXT_ELLIPSIS_LEN = "pickerTextEllipsisLen";
106112

113+
private static final String PICKER_FONT_FAMILY = "pickerFontFamily";
114+
107115
private static final String PICKER_EVENT_NAME = "pickerEvent";
108116
private static final String EVENT_KEY_CONFIRM = "confirm";
109117
private static final String EVENT_KEY_CANCEL = "cancel";
@@ -340,6 +348,32 @@ public void onSelected(ArrayList<ReturnData> selectedList) {
340348
break;
341349
}
342350

351+
if (options.hasKey(PICKER_FONT_FAMILY)) {
352+
Typeface typeface = null;
353+
AssetManager assetManager = activity.getApplicationContext().getAssets();
354+
final String fontFamily = options.getString(PICKER_FONT_FAMILY);
355+
try {
356+
String path = FONTS + fontFamily + TTF;
357+
typeface = Typeface.createFromAsset(assetManager, path);
358+
} catch (Exception ignored) {
359+
try {
360+
String path = FONTS + fontFamily + OTF;
361+
typeface = Typeface.createFromAsset(assetManager, path);
362+
} catch (Exception ignored2) {
363+
try {
364+
typeface = Typeface.create(fontFamily, Typeface.NORMAL);
365+
} catch (Exception ignored3) {
366+
}
367+
}
368+
}
369+
cancelTV.setTypeface(typeface);
370+
titleTV.setTypeface(typeface);
371+
confirmTV.setTypeface(typeface);
372+
373+
pickerViewAlone.setTypeface(typeface);
374+
pickerViewLinkage.setTypeface(typeface);
375+
}
376+
343377
if (options.hasKey(SELECTED_VALUE)) {
344378
ReadableArray array = options.getArray(SELECTED_VALUE);
345379
String[] selectedValue = getSelectedValue(array);

android/src/main/java/com/beefe/picker/view/LoopView.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ public void setTextColor(int color){
203203
invalidate();
204204
}
205205

206+
public void setTypeface(Typeface typeface){
207+
paintOuterText.setTypeface(typeface);
208+
paintCenterText.setTypeface(typeface);
209+
invalidate();
210+
}
211+
206212
public final void setNotLoop() {
207213
isLoop = false;
208214
}

android/src/main/java/com/beefe/picker/view/PickerViewAlone.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.beefe.picker.view;
22

33
import android.content.Context;
4+
import android.graphics.Typeface;
45
import android.util.AttributeSet;
56
import android.view.LayoutInflater;
67
import android.view.View;
@@ -212,6 +213,17 @@ public void setTextSize(float size){
212213
}
213214
}
214215

216+
public void setTypeface(Typeface typeface){
217+
int viewCount = pickerViewAloneLayout.getChildCount();
218+
for (int i = 0; i < viewCount; i++) {
219+
View view = pickerViewAloneLayout.getChildAt(i);
220+
if (view instanceof LoopView) {
221+
LoopView loopView = (LoopView) view;
222+
loopView.setTypeface(typeface);
223+
}
224+
}
225+
}
226+
215227
public void setTextEllipsisLen(int len){
216228
int viewCount = pickerViewAloneLayout.getChildCount();
217229
for (int i = 0; i < viewCount; i++) {

android/src/main/java/com/beefe/picker/view/PickerViewLinkage.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.beefe.picker.view;
22

33
import android.content.Context;
4+
import android.graphics.Typeface;
45
import android.util.AttributeSet;
56
import android.view.LayoutInflater;
67
import android.view.View;
@@ -603,6 +604,20 @@ public void setTextSize(float size){
603604
}
604605
}
605606

607+
public void setTypeface(Typeface typeface){
608+
switch (curRow) {
609+
case 2:
610+
loopViewOne.setTypeface(typeface);
611+
loopViewTwo.setTypeface(typeface);
612+
break;
613+
case 3:
614+
loopViewOne.setTypeface(typeface);
615+
loopViewTwo.setTypeface(typeface);
616+
loopViewThree.setTypeface(typeface);
617+
break;
618+
}
619+
}
620+
606621
public void setTextEllipsisLen(int len){
607622
switch (curRow) {
608623
case 2:

0 commit comments

Comments
 (0)