单TextView中多字体多字、TextView

由网友(倦了轻狂少年)分享简介:我想设置的TextView 的第一个字符用字体,第二个字符与不同类型的脸等等。我读到这个例子:Spannable STR =(Spannable)textView.getText();str.setSpan(新StyleSpan(android.graphics.Typeface.ITALIC),0,7,Spann...

我想设置的TextView 的第一个字符用字体,第二个字符与不同类型的脸等等。 我读到这个例子:

  Spannable STR =(Spannable)textView.getText();
str.setSpan(新StyleSpan(android.graphics.Typeface.ITALIC),0,7
                             ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 

但它并不能帮助我,因为我要设置多个字体(外部专题信托基金) 任何想法??

解决方案

使用下列code:(我用孟加拉语和泰米尔语字体)

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

的结果是:

CustomTypefaceSpan类:

 包my.app;
进口android.graphics.Paint;
进口android.graphics.Typeface;
进口android.text.TextPaint;
进口android.text.style.TypefaceSpan;

公共类CustomTypefaceSpan扩展TypefaceSpan {

私人最终字样NEWTYPE;

公共CustomTypefaceSpan(字符串的家庭,字体类型){
    超(家庭);
    NEWTYPE =类型;
}

@覆盖
公共无效updateDrawState(TextPaint DS){
    applyCustomTypeFace(DS,NEWTYPE);
}

@覆盖
公共无效updateMeasureState(TextPaint漆){
    applyCustomTypeFace(油漆,NEWTYPE);
}

私有静态无效applyCustomTypeFace(油漆涂料,字体TF){
    INT旧式;
    字样旧= paint.getTypeface();
    如果(旧== NULL){
        旧式= 0;
    } 其他 {
        旧式= old.getStyle();
    }

    INT假=旧式和放大器; 〜tf.getStyle();
    如果((假&安培;!Typeface.BOLD)= 0){
        paint.setFakeBoldText(真正的);
    }

    如果((假&安培;!Typeface.ITALIC)= 0){
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(TF);
}
}
 
Android能用px设置字体大小吗

Reference

I want to set the first character on TextView with a TypeFace and the second character with a different Type face and so on. I read this example:

Spannable str = (Spannable) textView.getText();
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7  
                             ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

but it didn't help me, because I want to set multiple TypeFace (external TTFs) Any idea??

解决方案

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:

CustomTypefaceSpan Class:

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

Reference

阅读全文

相关推荐

最新文章