Created
July 30, 2017 10:44
-
-
Save thanhbinh84/c36f930c1978e4f0d949c8f9a7662495 to your computer and use it in GitHub Desktop.
Customize TextInputLayout for workaround the issue https://github.com/chrisjenx/Calligraphy/issues/201
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.annotation.Nullable; | |
import android.support.design.widget.TextInputLayout; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.text.Spanned; | |
import android.text.TextUtils; | |
import android.util.AttributeSet; | |
import uk.co.chrisjenx.calligraphy.CalligraphyTypefaceSpan; | |
import static se.bghrt.bigheart.utils.Utils.getTypeFace; | |
/** | |
* Customize TextInputLayout for workaround the issue https://github.com/chrisjenx/Calligraphy/issues/201 | |
*/ | |
public class CalligraphyTextInputLayout extends TextInputLayout { | |
private Context mContext; | |
public CalligraphyTextInputLayout(Context context) { | |
this(context, null, 0); | |
} | |
public CalligraphyTextInputLayout(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CalligraphyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
mContext = context; | |
} | |
@Override | |
public void setError(@Nullable CharSequence error) { | |
Spannable spannable = null; | |
if (!TextUtils.isEmpty(error)) | |
spannable = wrapInCustomFont(mContext, error.toString()); | |
super.setError(spannable); | |
} | |
private Spannable wrapInCustomFont(Context context, String text) { | |
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(getTypeFace(context)); | |
SpannableString spannable = new SpannableString(text); | |
spannable.setSpan(typefaceSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | |
return spannable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why SPAN_EXCLUSIVE_EXCLUSIVE? Shouldn't it be SPAN_INCLUSIVE_EXCLUSIVE?