无图无真相,O(∩_∩)O哈哈~
这种效果一个TextView就可以搞定,多种字体样式,多种颜色,多种字体大小
废话不多说,直接上代码:
/**
*@date on 2019/4/25
*@author kelina
*@describe 自定义TextView,实现同个TextView使用多种字体样式
*/
public class MultTypeFaceTextView extends android.support.v7.widget.AppCompatTextView {
private List<CustomTypefaceSpan> customTypefaceSpans = new ArrayList<>();
private List<String> spans= new ArrayList<>();
public MultTypeFaceTextView(Context context) {
super(context);
}
public MultTypeFaceTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MultTypeFaceTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 设置字体样式
* @param customTypefaceSpan typeFace、文字缩放大小,颜色id
* @param span 需要修改字体样式的文字
*/
public void setTypeFaceSpan(CustomTypefaceSpan customTypefaceSpan,String span){
this.customTypefaceSpans.add(customTypefaceSpan);
this.spans.add(span);
// setText(customTypefaceSpan.getFamily());
}
@Override
public void setText(CharSequence text, BufferType type) {
int index;
if(!TextUtils.isEmpty(text) && spans!=null){
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
for(int i=0,length = spans.size();i<length;i++){
index = text.toString().indexOf(spans.get(i));
if(index != -1){
ssb.setSpan(customTypefaceSpans.get(i),index,index+spans.get(i).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
super.setText(ssb, type);
}
}
}
/**
* @author kelina
* @date on 2019/4/25
* @describe 自定字体样式
*/
public class CustomTypefaceSpan extends TypefaceSpan {
private Typeface typeface;
private float scaleTextSize = 1; //字体大小倍数
private int color = -99;//字体颜色
/**
* @param content TextView的内容
* @param type 字体样式
*/
public CustomTypefaceSpan(String content, Typeface type) {
super(content);
typeface = type;
}
/**
* @param content TextView的内容
* @param type 字体样式
* @param scaleTextSize 文字大小的倍数
*/
public CustomTypefaceSpan(String content, Typeface type, float scaleTextSize) {
super(content);
typeface = type;
this.scaleTextSize = scaleTextSize;
}
/**
* @param content TextView的内容
* @param type 字体样式
* @param scaleTextSize 文字大小的倍数
* @param color 文字的颜色
*/
public CustomTypefaceSpan(String content, Typeface type, float scaleTextSize, int color) {
super(content);
typeface = type;
this.scaleTextSize = scaleTextSize;
this.color = color;
}
/**
* @param content TextView的内容
* @param type 字体样式
* @param color 文字的颜色
*/
public CustomTypefaceSpan(String content, Typeface type, int color) {
super(content);
typeface = type;
this.color = color;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint);
}
private void applyCustomTypeFace(Paint paint) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
Typeface newTf = Typeface.create(typeface, oldStyle);
int fake = oldStyle & ~newTf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTextSize(paint.getTextSize() * scaleTextSize);
if (color != -99) {
paint.setColor(color);
}
paint.setTypeface(typeface);
}
}
调用方法:
String s="一生一世:1314,我爱你:520,气死气死你:74740";
MultTypeFaceTextView multtypefaceTextViewID = findViewById(R.id.multtypefaceTextViewID);
multtypefaceTextViewID.setTypeFaceSpan(new CustomTypefaceSpan(s, Typeface.createFromAsset(getAssets(), "go.OTF"),1.8f,getResources().getColor(R.color.colorAccent)),"1314");
multtypefaceTextViewID.setTypeFaceSpan(new CustomTypefaceSpan(s,Typeface.createFromAsset(getAssets(), "font.TTF"),1.5f,getResources().getColor(R.color.colorAccent)),"520");
multtypefaceTextViewID.setTypeFaceSpan(new CustomTypefaceSpan(s,Typeface.createFromAsset(getAssets(), "TT1018M_.TTF"),1.0f,getResources().getColor(R.color.colorAccent)),"74740");
multtypefaceTextViewID.setText(s);