Using TypeFace in Android
In android, when you using the custom fonts. You need to create the assets folder in the main, then paste your .ttf or .otf format fonts.
Here some methods will be very useful for you,
Set the typeface
public Typeface setTypeFace(Context context) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/MyriadPro_Regular.otf");
return typeface;
}
txt_username.setTypeface(setTypeFace(getActivity()));
Like you can set the EditText, Spinner and other widgets.
Using the spannable text view in android,
Spannable span = new SpannableString("Hello Android, Have a great day!");
span.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(Color.RED), 13, 25, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span);
Using Custom TypeFace:
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
import android.util.TypedValue;
public class CustomTypeFaceSpan extends TypefaceSpan {
private final Typeface newType;
private final int newSize;
public CustomTypeFaceSpan(String family, Typeface type, int size) {
super(family);
newType = type;
newSize = size;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType, newSize);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType, newSize);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf, int size) {
try {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTextSize(getPixelsFromDip(size));
paint.setTypeface(tf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static float getPixelsFromDip(float dip) {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dip,
Resources.getSystem().getDisplayMetrics()
);
}
}
Here some methods will be very useful for you,
Set the typeface
public Typeface setTypeFace(Context context) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/MyriadPro_Regular.otf");
return typeface;
}
txt_username.setTypeface(setTypeFace(getActivity()));
Like you can set the EditText, Spinner and other widgets.
Using the spannable text view in android,
Spannable span = new SpannableString("Hello Android, Have a great day!");
span.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(Color.RED), 13, 25, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span);
Using Custom TypeFace:
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
import android.util.TypedValue;
public class CustomTypeFaceSpan extends TypefaceSpan {
private final Typeface newType;
private final int newSize;
public CustomTypeFaceSpan(String family, Typeface type, int size) {
super(family);
newType = type;
newSize = size;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType, newSize);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType, newSize);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf, int size) {
try {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTextSize(getPixelsFromDip(size));
paint.setTypeface(tf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static float getPixelsFromDip(float dip) {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dip,
Resources.getSystem().getDisplayMetrics()
);
}
}
Comments