Apply Two Different Font Styles to a Textview

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 );

TextView with different fonts and styles?

You can do this using:

textView.setText(Html.fromHtml("<b>myLogin</b> <i>logout</i>"));

For more options, look into SpannableString: Link

With SpannableString, you can apply multiple formatting to a single string.

This article will be very helpful to you: Rich-Style Formatting of an Android TextView

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);

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!

How to add two different font size to textview text in swift

You can do that using attributedText.

try like this

var heading = "Bills or Taxes once paid through the payment gateway shall not be refunded other then in the following circumstances:"
var content = "\n \n 1. Multiple times debiting of Consumer Card/Bank Account due to ticnical error excluding Payment Gateway charges would be refunded to the consumer with in 1 week after submitting complaint form. \n \n 2. Consumers account being debited with excess amount in single transaction due to tecnical error will be deducted in next month transaction. \n \n 3. Due to technical error, payment being charged on the consumers Card/Bank Account but the Bill is unsuccessful."

let attributedText = NSMutableAttributedString(string: heading, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20)])

attributedText.append(NSAttributedString(string: content, attributes: [NSAttributedStringKey.font: UIFont.SystemFont(ofSize: 15), NSAttributedStringKey.foregroundColor: UIColor.blue]))

refundTextview.attributedText = attributedText

Is it possible to have multiple styles inside a TextView?

In case, anyone is wondering how to do this, here's one way: (Thanks to Mark again!)

mBox = new TextView(context);
mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br />" +
"<small>" + description + "</small>" + "<br />" +
"<small>" + DateAdded + "</small>"));

For an unofficial list of tags supported by this method, refer to this link or this question: Which HTML tags are supported by Android TextView?



Related Topics



Leave a reply



Submit