0

I have a custom class as follows:

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

I apply the span as follows:

Spannable spannable = new SpannableString(dispStatsPart1()+"\n"+dispStatswords()+"Months"); 
                        spannable.setSpan( new CustomTypefaceSpan("sans-serif",Helv_cond_bold), 0, dispStatsPart1().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        spannable.setSpan( new CustomTypefaceSpan("sans-serif",helv_light), dispStatsPart1().length(), dispStatsPart1().length() +(dispStatswords()+"Months").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        holder.numberText.setText(spannable);

I want a different font size for the first line and a different font size for the second line, How to achieve that?

So far I have tried modifying my code to:

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);
        }
        if(flag == 0){
            paint.setTextSize(50);
        }else{
            paint.setTextSize(50); 
        }

        paint.setTypeface(tf);
    }

I pass the flags to the constructor:

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

But the above does not seem to work, the font size is increased or decreased but it happens for both strings.

User3
  • 2,465
  • 8
  • 41
  • 84
  • Refer this http://stackoverflow.com/questions/16335178/different-size-of-strings-in-the-same-textview – Remees M Syde Oct 29 '14 at 10:15
  • WHile your answer is helpful, but I am using a custom class and I want to know the implementation in that context. – User3 Oct 29 '14 at 10:24

2 Answers2

2

If you're wondering how you can set multiple different sizes in the same textview, but using an absolute size and not a relative one, you can achieve that using AbsoluteSpan.

dimens.xml

<dimen name="text_size_1">18sp</dimen>
<dimen name="text_size_2">12sp</dimen>

Just get the dimension in pixels of the desired text size

int textSize1 = getResources().getDimensionPixelSize(R.dimen.text_size_1);
int textSize2 = getResources().getDimensionPixelSize(R.dimen.text_size_2);

and then create a new AbsoluteSpan based on the text

String text1 = "text1 font size is 18sp";
String text2 = "text2 font size is 12sp ";

SpannableString span1 = new SpannableString(text1);
span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE);

SpannableString span2 = new SpannableString(text2);
span2.setSpan(new AbsoluteSizeSpan(textSize2), 0, text2.length(), SPAN_INCLUSIVE_INCLUSIVE);

// let's put both spans together with a separator and all
CharSequence finalText = TextUtils.concat(span1, "\n", span2);

Here is the output:

Two different font sizes for two strings in one single text view

Ahsan Kamal
  • 1,085
  • 2
  • 13
  • 34
0
yourTextView.setText(Html.fromHtml("xxx"))    
zhaokun
  • 104
  • 2
  • 10