Multiple Typeface in Single Textview

Multiple TypeFace in single TextView

Use the following code:(I'm using Bangla and Tamil font)

  TextView txt = (TextView) findViewById(R.id.custom_fonts);  
txt.setTextSize(30);
Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf");
SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
txt.setText(SS);

The outcome is:

Sample Image


This uses the CustomTypefaceSpan class, taken from How can I use TypefaceSpan or StyleSpan with a custom Typeface?:


package my.app;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
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.setTypeface(tf);
}
}

Multiple fonts to a single custom TextView

This is how I achieved:

  1. Created a custom TextView

    public class CaptainTextView extends TextView {
    private HashMap<String, Typeface> mTypefaces;

    public CaptainTextView(Context context) {
    super(context);
    }

    public CaptainTextView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    super(context, attrs, defStyleAttr);
    if (mTypefaces == null) {
    mTypefaces = new HashMap<>();
    }

    if (this.isInEditMode()) {
    return;
    }

    final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CaptainTextView);
    if (array != null) {
    final String typefaceAssetPath = array.getString(
    R.styleable.CaptainTextView_customTypeface);

    if (typefaceAssetPath != null) {
    Typeface typeface;

    if (mTypefaces.containsKey(typefaceAssetPath)) {
    typeface = mTypefaces.get(typefaceAssetPath);
    } else {
    AssetManager assets = context.getAssets();
    typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
    mTypefaces.put(typefaceAssetPath, typeface);
    }

    setTypeface(typeface);
    }
    array.recycle();
    }
    }

    public CaptainTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    if (mTypefaces == null) {
    mTypefaces = new HashMap<>();
    }

    if (this.isInEditMode()) {
    return;
    }

    final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CaptainTextView);
    if (array != null) {
    final String typefaceAssetPath = array.getString(
    R.styleable.CaptainTextView_customTypeface);

    if (typefaceAssetPath != null) {
    Typeface typeface;

    if (mTypefaces.containsKey(typefaceAssetPath)) {
    typeface = mTypefaces.get(typefaceAssetPath);
    } else {
    AssetManager assets = context.getAssets();
    typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
    mTypefaces.put(typefaceAssetPath, typeface);
    }

    setTypeface(typeface);
    }
    array.recycle();
    }
    }
    }
  2. Declared a custom attribute

    <resources>
    <declare-styleable name="CaptainTextView">
    <attr name="customTypeface" format="string" />
    </declare-styleable>
    </resources>
  3. Used in XML

    <com.project.captain.customviews.CaptainTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome"
    android:textSize="16sp"
    android:textStyle="bold"
    app:customTypeface="fonts/Roboto-Thin.ttf" />

Voila!

Is it possible to have multiple Typefaces in the same TextView?

Actually it looks like you can do it with the help of a Spannable:

String text = "This is an example";

Spannable s = new SpannableString(text + " text");
s.setSpan(new TypefaceSpan("monospace"), 0, text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new TypefaceSpan("serif"), text.length(), s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

((TextView)findViewById(R.id.my_text_view)).setText(s);

Apply two different font styles to a TextView

One way to do this is to extend TypefaceSpan:

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
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.setTypeface(tf);
}
}

Then when you want to use two different typefaces call:

String firstWord = "first ";
String secondWord = "second";

// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);

// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Set the text of a textView with the spannable object
textView.setText( spannable );

set different fonts inside TextView

For this you need to use a custom TypefaceSpan

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
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.setTypeface(tf);
}
}

Usage

        TextView txt = (TextView) findViewById(R.id.custom_fonts);  

Typeface font1 = Typeface.createFromAsset(getActivity().getAssets(),"fonts/firstFont.otf");
Typeface font2 = Typeface.createFromAsset(getActivity().getAssets(),"fonts/secondFont.otf");

SpannableStringBuilder spanString = new SpannableStringBuilder("Hello, i'm textview content");

spanString.setSpan(new CustomTypefaceSpan("", font1), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spanString.setSpan(new CustomTypefaceSpan("", font), 4, 26,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
txt.setText(spanString);

How do I add multiple fonts to font text view class?

Use the following code for different fonts set to the xml file.

public class CustomTextView extends TextView {
private static final String TAG = "CustomTextView";

public CustomTextView(Context context) {
super(context);
}

public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}

private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTextView);
String customFont = a.getString(R.styleable.CustomTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}

public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/"+asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}

setTypeface(tf);
return true;
}

}

And in the xml file you can use it as:

<com.package_name.CustomTextView
your_name:customFont="arialbd.ttf" />

and int the main parent layout add

xmlns:your_name="http://schemas.android.com/apk/res/com.package_name"

and remember to add a attrs.xml in values folder, with following resource in it

<resources>
<declare-styleable name="CustomTextView">
<attr name="customFont" format="string"/>
</declare-styleable>

Hope it helps

Multiple font to single textview depending on langugage

Found this:

String firstString = "AAAAAA: ";
String secondString = "";

SpannableStringBuilder sb = new SpannableStringBuilder(firstString + secondString);

sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, firstString.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);

sb.setSpan(new ForegroundColorSpan(Color.rgb(255, 0, 0)), firstString.length() + 1,
firstString.length() + secondString.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);

textView.setText(sb);

might work



Related Topics



Leave a reply



Submit