|
13 | 13 | import android.graphics.drawable.RippleDrawable;
|
14 | 14 | import android.graphics.drawable.StateListDrawable;
|
15 | 15 | import android.os.Build;
|
| 16 | +import android.support.annotation.FontRes; |
| 17 | +import android.support.v4.content.res.ResourcesCompat; |
16 | 18 | import android.util.AttributeSet;
|
17 | 19 | import android.view.Gravity;
|
18 | 20 | import android.view.View;
|
@@ -360,12 +362,31 @@ private void initAttributesArray(TypedArray attrsArray) {
|
360 | 362 | ? Utils.findFont(mContext, iconFontFamily, mDefaultIconFont)
|
361 | 363 | : Utils.findFont(mContext, mDefaultIconFont, null);
|
362 | 364 |
|
363 |
| - mTextTypeFace = textFontFamily != null |
364 |
| - ? Utils.findFont(mContext, textFontFamily, mDefaultTextFont) |
365 |
| - : Utils.findFont(mContext, mDefaultTextFont, null); |
| 365 | + Typeface fontResource = getTypeface(attrsArray); |
| 366 | + if (fontResource != null) { |
| 367 | + mTextTypeFace = fontResource; |
| 368 | + } else { |
| 369 | + mTextTypeFace = textFontFamily != null |
| 370 | + ? Utils.findFont(mContext, textFontFamily, mDefaultTextFont) |
| 371 | + : Utils.findFont(mContext, mDefaultTextFont, null); |
| 372 | + } |
366 | 373 | }
|
367 | 374 | }
|
368 | 375 |
|
| 376 | + private Typeface getTypeface(TypedArray ta) { |
| 377 | + if (ta.hasValue(R.styleable.FancyButtonsAttrs_android_fontFamily)) { |
| 378 | + int fontId = ta.getResourceId(R.styleable.FancyButtonsAttrs_android_fontFamily, 0); |
| 379 | + if (fontId != 0) |
| 380 | + return ResourcesCompat.getFont(getContext(), fontId); |
| 381 | + } |
| 382 | + if(ta.hasValue(R.styleable.FancyButtonsAttrs_fb_textFontRes)){ |
| 383 | + int fontId = ta.getResourceId(R.styleable.FancyButtonsAttrs_fb_textFontRes, 0); |
| 384 | + if (fontId != 0) |
| 385 | + return ResourcesCompat.getFont(getContext(), fontId); |
| 386 | + } |
| 387 | + return null; |
| 388 | + } |
| 389 | + |
369 | 390 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
370 | 391 | private Drawable getRippleDrawable(Drawable defaultDrawable, Drawable focusDrawable, Drawable disabledDrawable) {
|
371 | 392 | if (!mEnabled) {
|
@@ -807,6 +828,22 @@ public void setCustomTextFont(String fontName) {
|
807 | 828 | mTextView.setTypeface(mTextTypeFace, textStyle);
|
808 | 829 | }
|
809 | 830 |
|
| 831 | + /** |
| 832 | + * Set custom font for button Text |
| 833 | + * |
| 834 | + * @param fontId : Font id |
| 835 | + * Place your text fonts in font resources. |
| 836 | + * Eg. res/font/roboto.ttf or res/font/roboto.xml |
| 837 | + */ |
| 838 | + public void setCustomTextFont(@FontRes int fontId) { |
| 839 | + mTextTypeFace = ResourcesCompat.getFont(getContext(), fontId); |
| 840 | + |
| 841 | + if (mTextView == null) |
| 842 | + initializeFancyButton(); |
| 843 | + else |
| 844 | + mTextView.setTypeface(mTextTypeFace, textStyle); |
| 845 | + } |
| 846 | + |
810 | 847 | /**
|
811 | 848 | * Set Custom font for button icon
|
812 | 849 | *
|
|
0 commit comments